【问题标题】:if I add anything inside componentWillUnmount nothing happening如果我在 componentWillUnmount 中添加任何内容,则不会发生任何事情
【发布时间】:2019-05-20 09:18:10
【问题描述】:
  • 我正在努力学习自己的反应。
  • 我正在尝试在我的 componentWillUnmount 中设置会话。
  • 但如果我在 componentWillUnmount 中添加任何内容,则不会发生任何事情。
  • 我通过添加控制台和调试器进行调试仍然没有任何反应。
  • 你们能告诉我如何让我的 componentWillUnmount 工作
  • 以便将来我自己修复它。
  • 在下面提供我的相关代码 sn-p 和沙箱。
  • 我所有的代码都在 RecipeReviewCard.js 中

https://codesandbox.io/s/1vqkz1own7

 componentDidMount() {
    console.log("componentDidMount---->");
  }

  componentWillUnmount() {
    console.log("componentwillUnmount---->");
    debugger;
    window.sessionStorage.setItem(
      "favoriteValues",
      JSON.stringify(this.props.benchMarks)
    );
  }

【问题讨论】:

  • 我在更改标签时看到控制台日志 'componentwillUnmount----->' - 似乎工作正常
  • 你能把与问题相关的代码贴出来吗?
  • componentWillUnmount 是在组件销毁之前调用的,不是用来创建会话的。
  • @VelimirTchatchevsky 嘿,当我们刷新 componentwillUnmount 页面时,有什么办法可以让它工作
  • 好像你想要componentWillMount - 在组件显示之前触发

标签: javascript html reactjs redux material-ui


【解决方案1】:

顾名思义,“componentWillUnmount”将在组件即将从 DOM 中取出时触发(例如,当您切换选项卡时),在您的示例中,console.log 确实会触发。当组件加载到 DOM 时,使用 'componentWillMount' 运行函数

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 2015-08-10
相关资源
最近更新 更多