2 回答

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
首先這里有一個(gè)很大的概念錯(cuò)誤:
if (this._tarefaInput.value !== ""){
state.list.push({ text: this._taskInput.value });
this.setState(state);
}
您正在使用該推送功能直接編輯狀態(tài),您不應(yīng)該在反應(yīng)中這樣做,因?yàn)樗鼤?huì)導(dǎo)致意想不到的后果,這就是您應(yīng)該如何更新狀態(tài):
if (this._tarefaInput.value !== ""){
//use the spread operator (...) to create a copy of the list in the state
const newList = [... state.list];
// push the new element tot the new list
newList.push({ text: this._taskInput.value });
// update the state
this.setState({list: newList});
}
現(xiàn)在你得到的錯(cuò)誤很可能發(fā)生,因?yàn)樵谀愕拇a中的某個(gè)地方(可能在里面<Table/>)你試圖將列表數(shù)組的每個(gè)元素打印為一個(gè)反應(yīng)組件。您尚未共享列表呈現(xiàn)的部分,但我猜您正在做這樣的事情:
//somewhere inside a render:
{
list.map(Element => <Element />);
}
//Proper way of doing it
{
list.map(element => <p>{element.text}</p>);
}
如果您共享更多代碼和帶有錯(cuò)誤描述的整個(gè)日志(帶有文件和行號(hào)),我可以嘗試提供更多幫助

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
問題是您的渲染中的這一行:{this.state.list}
。你可以渲染一個(gè)數(shù)組,但你不能渲染一個(gè)對(duì)象。解決方案是映射數(shù)組并輸出一些 JSX,如下所示。假設(shè)您有一個(gè)具有 aname
和id
屬性的對(duì)象列表:
{this.state.list.map(item => (<div key={item.id}>{item.id}</div>))}
添加回答
舉報(bào)