【问题标题】:Javascript object destructoring not working within react this.propsJavascript对象解构在react this.props中不起作用
【发布时间】:2019-05-01 12:43:43
【问题描述】:

几天前我刚刚了解了使用简单对象进行对象析构的知识,并且效果很好。但是,当我尝试在 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(this.props.currentTeam.metaDataUpdated) 会得到什么?我问,因为可能是控制台输出this.props.currentTeam时的延迟加载效果。
  • 好问题。这也给出了 undefined 所以我猜想通过 redux 更新我的商店的某个地方效果不佳。感谢您为我指明正确的方向。
  • @trincot:发现问题了,我忘了在reducer中以不可变的方式复制我的状态。 (Y)
  • 很高兴听到您解决了问题!

标签: javascript reactjs


【解决方案1】:

您的console.log 语句正在查看两个不同的事物,而console.log 在显示特定时间点的事物时并不可靠。如果您想调试这样的东西,请尝试使用它:

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

然后您可能会看到值的一致性,您的问题很可能与 React 生命周期或您更新道具或状态的方式有关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    相关资源
    最近更新 更多