關(guān)于箭頭函數(shù)
//?render函數(shù) render()?{????return?(??????<div?className="TodoList">????????<input?value={this.state.inputValue}?type="text"?onChange={this.handleChange}?/>????????<button?onClick={this.handleClick}>add</button>????????<ul>??????????{????????????this.state.list.map((item,?index)?=>?{??????????????//?get?the?index?of?list,?any?other?great?way?to?solve?this???????????????//?return?<li?key={index}?onClick={this.handleItemClick.bind(this,?index)}>{item}</li>??????????????return?<li?key={index}?onClick={this.handleItemClick(index)}>{item}</li>????????????})??????????}????????</ul>??????</div>????);??} //?點(diǎn)擊刪除函數(shù) ??handleItemClick?=?(index)?=>?{????const?list?=?[...this.state.list];????list.splice(index,?1);????this.setState({??????list:?list????});??}
如果使用箭頭函數(shù)的方法,會(huì)在添加的時(shí)候瞬間刪除,并且拋出index.js:1 Warning: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.的錯(cuò)誤。
說(shuō)是是由于引用的子組件,通過(guò)props進(jìn)行傳遞了 ,傳遞的過(guò)程中實(shí)際上已經(jīng)處于render階段了 ,在這個(gè)階段 如果你再改變這個(gè)state值的話,就會(huì)包這個(gè)錯(cuò) 。
請(qǐng)問(wèn)有什么辦法去解決?
2020-07-27
抱歉 上面的格式錯(cuò)誤 重新傳一下代碼片段