标签: react-component-unmount

React 组件状态在组件卸载之前被擦除

如果我返回一个函数,useEffect我可以确定该函数将在组件卸载时运行。但 React 似乎在调用我的卸载函数之前擦除了本地状态。

考虑一下:

function Component () {

  const [setting, setSetting] = useState(false)

  useEffect(() => {

    setSetting(true)

    // This should be called when unmounting component
    return () => {

      console.log('Send setting to server before component is unmounted')
      console.log(setting) // false (expecting setting to be true)
      
    }
  }, [])
  
  return (
    <p>Setting is: {setting ? 'true' : 'false'}</p>
  )
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以确认预期的行为是应该擦除组件状态吗?而且,如果这是正确的行为,那么如何在卸载组件之前将当前组件状态发送到服务器?

为了提供一些上下文,我正在消除对服务器的发布请求,以避免每次用户更改设置时都会触发它。去抖动效果很好,但我需要一种在用户离开页面后触发请求的方法,因为排队的去抖动方法将不再从未安装的组件中触发。

debouncing reactjs use-effect use-state react-component-unmount

4
推荐指数
1
解决办法
1607
查看次数