V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
erwin985211
V2EX  ›  问与答

求思路, 如何解决 react hook 使用 generator function 报的组件销毁依然改变 state 的错误。

  •  
  •   erwin985211 · Oct 14, 2021 · 816 views
    This topic created in 1666 days ago, the information mentioned may be changed or developed.

    大致代码如下

    dataSource = React.useMemo(一个 generator function)

    let faultState = useInfiniteVirtualScroll({ dataSource, }) // 自定义 hook 。

    retrue ( 根据 faultState 渲染的 jsx)

    大致就是这些代码,在组件的 generator function 还未完成时销毁组件,react 会报一个

    /Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function./

    就是组件都销毁你还改变 state,目前尝试

    React.useEffect(() => {
        return () => {
            faultState= null as any
            dataSource = null as any
        }
    }, []) 
    
    没有效果,求思路
    
    erwin985211
        2
    erwin985211  
    OP
       Oct 14, 2021
    @4ark 思路确实是这样了,我仔细找了是我找的组件的问题已经给它提 bug
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1311 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 23:49 · PVG 07:49 · LAX 16:49 · JFK 19:49
    ♥ Do have faith in what you're doing.