数据展示
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) }
正常修改数据