[英]Javascript object destructoring not working within react this.props
幾天前我剛剛用簡單的對象學習了對象析構,而且效果很好。 然而,當我嘗試在 this.props 上使用它時,它為我的所有變量返回 undefined,我不確定我在這里做錯了什么。 任何幫助深表感謝。 干杯。
render() {
console.log("the properties from props", this.props.currentTeam);
const {
metaDataUpdated,
metaDataUpdating,
needsToResetUpdateMessage
} = this.props.currentTeam;
console.log(
`metadata updated? ${metaDataUpdated} --- is metaDataUpdating? ${metaDataUpdating} --- need to update message ${needsToResetUpdateMessage}`
);
您的console.log
語句正在查看兩個不同的內容,並且console.log
在顯示特定時間點的內容時不可靠。 如果您想調試這樣的東西,請嘗試使用它:
console.log(JSON.stringify(this.props.currentTeam)));
console.log(JSON.stringify(metaDataUpdated));
然后你可能會看到值的一致性,你的問題很可能與 React 生命周期或你更新 props 或 state 的方式有關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.