React中setState数据合并
作者:互联网
数据展示
constructor(props){ super(props); this.state={ name:['流行','新款','精选'], currentTitle:'流行', increment:"Hello,World", title:'coderWhy' } } render(){ const {name,currentTitle,increment,title} =this.state return( <div> <CurrentBar itemClick={index=>this.itemClick(index)} titles={name}/> <h2>{currentTitle}</h2> <Home /> <About /> <Header/> <h2>当前的文本为:{increment}</h2> <h2>{title}</h2> <button onClick={e=>this.changText()}>改变文本</button> <button id="btn">改变文本2</button> </div> ) }
使用setState更改数据
changText(){ setTimeout(()=>{ this.setState({ increment:'Hello,World', title:'kobi' }) console.log(this.state.increment); },0) }
正常修改数据
标签:name,title,合并,currentTitle,React,state,increment,setState 来源: https://www.cnblogs.com/lpx520/p/15780781.html