zoukankan      html  css  js  c++  java
  • react todolist代码优化

    Todolist.js

    import React, { Component,Fragment } from 'react';
    import TodoItem from './TodoItem';
    import './style.css';
    class Todolist extends Component {
        constructor(props) { //最优先执行的函数
            super(props);
            this.state={
                inputValue:'',
                list:[]
            }
            this.handleinputChange=this.handleinputChange.bind(this);
            this.handlebuttonClick=this.handlebuttonClick.bind(this);
            this.handleItemdelt=this.handleItemdelt.bind(this);
        }
        render() {
            return ( 
                <Fragment>
                    <div>
                        {/*这是一个todolist*/}
                        <label htmlFor='insertArea'>输入内容</label>
                        <input 
                            id="insertArea"
                            className='input'
                            value={this.state.inputValue}
                            onChange={this.handleinputChange}
                        />
                        <button onClick={this.handlebuttonClick}> 提交 </button> 
                    </div> 
                    <ul>
                        {this.getTodoItem()}
                    </ul> 
                </Fragment>
            );
        }
        getTodoItem(){
            return this.state.list.map((item,index)=>{
                return(
                        <TodoItem 
                            key={index}
                            index={ index } 
                            item={ item } 
                            deleteItem={this.handleItemdelt}
                        />
                    )
            })
        }
        handleinputChange(e){
            const value=e.target.value;
            this.setState(()=>({
                inputValue:value
            }));
            // this.setState({
            //     inputValue:e.target.value
            // })
        }
        handlebuttonClick(e){
            this.setState((prevState)=>{
                return{
                    list:[...prevState.list,prevState.inputValue],
                    inputValue:''
                }
            });
            // this.setState({
            //     list:[...this.state.list,this.state.inputValue],
            //     inputValue:''
            // })
        }
        handleItemdelt(index){
            // immutable
            // state 不允许我们坐任何的改变
            this.setState((prevState)=>{
                const list=[...prevState.list]; 
                list.splice(index,1);
                return{list}
            })
            // const list=[...this.state.list];   // list的一个副本
            // list.splice(index,1);
            // this.setState({
            //     list:list
            // })
        }
    }
    export default Todolist;

    TodoItem.js

    import React ,{ Component } from 'react';
    class TodoItem extends Component{
        constructor(props){
            super(props);
            this.handleclick=this.handleclick.bind(this);
        }
        render(){
            const { item }=this.props;
            return (<li 
                onClick={this.handleclick}
                dangerouslySetInnerHTML={{__html:item}}
                ></li>
                )
        }
        handleclick(){
            const {deleteItem,index}=this.props;
            deleteItem(index);
        }
    }
    export default TodoItem;
  • 相关阅读:
    PostgreSQL中的partition-wise join
    Partition-wise join
    外观模式 门面模式 Facade 结构型 设计模式(十三)
    桥接模式 桥梁模式 bridge 结构型 设计模式(十二)
    组合模式 合成模式 COMPOSITE 结构型 设计模式(十一)
    创建型设计模式对比总结 设计模式(八)
    原型模式 prototype 创建型 设计模式(七)
    单例模式 创建型 设计模式(六)
    建造者模式 生成器模式 创建型 设计模式(五)
    抽象工厂模式 创建型 设计模式(四)
  • 原文地址:https://www.cnblogs.com/Lolita-web/p/9875642.html
Copyright © 2011-2022 走看看