美文网首页web开发
react - 留言板demo

react - 留言板demo

作者: 1994陈 | 来源:发表于2018-09-19 11:13 被阅读5次
    引入react,

    方法一:
    import React from 'react'
    方法二:
    import React,{component} from 'react'

    import ReactDom from 'react-dom'

    class App extends React.Component{
    //存数据的列表
    state={list:[]};
    //调整this指向
    constructor(){
    this.update=this.update.bind(this)
    };
    render(){
    return(
    //布局渲染
    <div>
    <input type="text" ref="name" />

    <input type="text" ref="content"/>

    <input type="button" value="留言" onClick={this.update}/>
    {this.state.list.map((item,index)=>{
    return <li key={item.id}>{item.name}/{item.content}</li>
    })}
    </div>
    )
    };
    update(){
    this.setState({
    list:this.state.list.concat({
    id:this.state.list.length+1,
    name:this.refs.name.value,
    content:this.refs.content.value
    })
    })
    }
    }

    ReactDom.render(
    <App/>,
    document.querySelect("#root")
    )

    相关文章

      网友评论

        本文标题:react - 留言板demo

        本文链接:https://www.haomeiwen.com/subject/krfvnftx.html