gpt4 book ai didi

reactjs - 记录 setState 调用

转载 作者:行者123 更新时间:2023-12-03 13:45:45 26 4
gpt4 key购买 nike

在 redux 中,您可以通过控制台记录 reducer 中收到的操作来调试状态问题,是否有类似的方法可以使用 setState api 来做到这一点?

我有一个相当大的抽象代码库并尝试调试某个问题,而不是仅仅在属性上查找 setState 调用那么简单

最佳答案

来自React文档:

  1. setState 将回调作为第二个参数

The second parameter to setState() is an optional callback function that will be executed once setState is completed and the component is re-rendered

因此,在回调函数中执行一些调试可能会对您有所帮助。

  • 另一种方法是使用comonentDidUpdate
  • componentDidUpdate() is invoked immediately after updating occurs. This method is not called for the initial render

    componentDidUpdate(prevProps, prevState)

    取决于你的逻辑,你可以在这里做同样的事情:

    componentDidUpdate(prevProps, prevState){
    if(this.state.foo !== prevState.foo){
    console.log('FOO')
    }
    }

    希望对你有帮助

    关于reactjs - 记录 setState 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48121478/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com