5 回答

TA貢獻1816條經驗 獲得超4個贊
**
如果您使用react,那么您應該始終使用setState 來更改狀態。
**
? ? this.setState({ // calling setState for updating the state
? ? ? ?result: this.state.result.map(item => {
? ? ? ? ? ?let tmpItem = {...item};// storing all the value here in tmpItem
? ? ? ? ? ?delete tmpItem.id; // deleting the key
? ? ? ? ? ?return {...tmpItem}; // returning here
? ? ? ?})
? ? }, ()=> {
? ? ? ? // for immediatly accessing the state you can use callback
? ? });

TA貢獻1842條經驗 獲得超13個贊
state在計算下一個狀態時,您不應該依賴:s 值,因為它可能會由 React 在幕后異步更新。
另外,請盡量避免,delete因為它是性能殺手。
所以試試這個:
this.setState((state, props) => ({
result: state.result.map(({id, ...propsToKeep}) => propsToKeep)
}));
或者如果你必須使用delete:
this.setState((state, props) => ({
result: state.result.map(res => {
delete res.id;
return res;
});
}));

TA貢獻1817條經驗 獲得超6個贊
在 React 中,您無法直接更新state. 你總是必須使用setState方法來做到這一點。
所以試試這個。
this.setState({
result: this.state.result.map(item => {
let tmpItem = {...item};
delete tmpItem.id;
return {...tmpItem};
})
});
謝謝!

TA貢獻1946條經驗 獲得超3個贊
在 React.js 中,你永遠不應該嘗試直接改變狀態。您必須使用 setState() 才能獲得結果。id 沒有被刪除,因為沒有發生重新渲染。
從文檔中,
不要直接修改狀態
//?Wrong this.state.comment?=?'Hello';
相反,使用 setState():
//?Correct this.setState({comment:?'Hello'});
添加回答
舉報