todolist

标题

import React, { Component } from 'react'

export default class App extends Component {

    state = {
        datalist:["aaa","bbb","ccc","ddddd"]
    }

    mytext = React.createRef() 

    render() {
        // console.log(this)
        return (
            <div>
                <input type="text" ref={this.mytext} />
                <button onClick={
                    this.handleClickAdd.bind(this)
                }>add2</button>
                    
                <ul>
                    {
                        this.state.datalist.map((item,index)=>
                        <li key={item}>
                            {item}
                            <button onClick={()=>this.handleDel(index)}>del1</button>
                            <button onClick={this.handleDel.bind(this,index)}>del2</button>
                        </li>
                        )
                    }
                </ul>
            </div>
        )
    }

    handleClickAdd() {
        console.log("click", this.mytext.current.value)

        // this.state.datalist.push(this.mytext.current.value)
        // this.forceUpdate()

        this.setState({
            datalist:[...this.state.datalist,this.mytext.current.value]
        })

        this.mytext.current.value = "" //ref 引用清空 
    }

    handleDel(index){

        // this.state.datalist.splice
        console.log("del",index)

        // this.state.datalist.splice(index,1)
        var newlist = [...this.state.datalist]
        newlist.splice(index,1)
        this.setState({
            datalist:newlist
        })
    }

}

var list= ["aaa","bbb","ccc"]

var newlist = list.map(item=>`<li>${item}</li>`)

console.log(newlist.join(""))

//r-if?----------
//r-show?---------
上一篇:C# 调用 Rust 编写的 dll 之二:输入输出简单数值


下一篇:LeetCode刷题——47. 全排列 II