我試圖創建一個簡單的收件箱,它使api調用並返回包含消息列表的json對象。然後通過道具傳遞給'InboxList',然後'InboxItem'組件。但是,我正在努力讓道具渲染每個項目。React - 將道具傳遞給'ES6 extends'組件,並正確使用'bind'
我在使用bind(this)時也收到錯誤,如下所示。
index.js:28 Uncaught (in promise) TypeError: (intermediate value).bind is not a function(…)
我認爲我需要我的componentDidMount方法中的綁定由於ES6語法,但我不明白的錯誤指。 Fwiw json數據成功迴歸。
對此有何線索將是非常讚賞
export default class Inbox extends Component {
constructor() {
super();
this.state = {
messages: [],
};
}
componentDidMount() {
this.serverRequest = axios.get('/api/messages')
.then(res => {
console.log(res.data);
})
.catch(res => {
if (res instanceof Error) {
console.log(res.message);
} else {
console.log(res.data);
}
this.setState({
messages: res.data,
}.bind(this));
});
}
render() {
return (
<div>
<InboxHeader />
<InboxList messages={this.state.messages} />
</div>
);
}
}
export default class InboxList extends Component {
render() {
return (
<ul className="dm-inbox__list">
{this.props.messages.map(message =>
<InboxItem message={message} />
)}
</ul>
);
}
}
正確答案是低於,但只是作爲一種精確,你所得到的是'綁定不一個函數',因爲你綁定了對象,而不是函數,所以this.setState({...})。bind,不會拋出上面的錯誤,但這不是真正的答案,但只是爲了解釋你所得到的具體錯誤。 – omerts