简体   繁体   English

Javascript 对象析构在 react this.props 中不起作用

[英]Javascript object destructoring not working within react this.props

I just learned about object destructoring a few days ago with simple object and that worked fine.几天前我刚刚用简单的对象学习了对象析构,而且效果很好。 However when I try to use it in react on this.props, it returns undefined for all my variables, I'm not really certain what I am doing wrong here.然而,当我尝试在 this.props 上使用它时,它为我的所有变量返回 undefined,我不确定我在这里做错了什么。 Any help is much appreciated.任何帮助深表感谢。 Cheers.干杯。

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}`
    );

爪哇

Your console.log statements are looking at two different things and console.log is not reliable at showing things at a specific point in time.您的console.log语句正在查看两个不同的内容,并且console.log在显示特定时间点的内容时不可靠。 If you want to debug things like this, try using this instead:如果您想调试这样的东西,请尝试使用它:

console.log(JSON.stringify(this.props.currentTeam)));
console.log(JSON.stringify(metaDataUpdated));

Then you'll probably see that there is consistency in the values and your problem is most likely something else having to do with the React lifecycle or the way you are updating the props or state.然后你可能会看到值的一致性,你的问题很可能与 React 生命周期或你更新 props 或 state 的方式有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM