Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

非global store互相引用时,报undefined错误 #98

Open
freewind opened this issue Sep 19, 2022 · 5 comments
Open

非global store互相引用时,报undefined错误 #98

freewind opened this issue Sep 19, 2022 · 5 comments

Comments

@freewind
Copy link

freewind commented Sep 19, 2022

复现repo: https://github.com/freewind-demos/typescript-react-hox-mix-stores-demo


useStore1.ts:

export const [useStore1, Store1Provider] = createStore(() => {
    const [user, setUser] = useState<string>();
    const {version} = useStore2();

    return {user, setUser: (user: string) => setUser(`${user}.${version}`)}
});

useStore2.ts

export const [useStore2, Store2Provider] = createStore(() => {
    const [version, setVersion] = useState(0)
    return {version, setVersion}
});

组件:

export const Hello: FC<Props> = ({}) => {
    return <Store1Provider>
        <Store2Provider>
            <Version/>
            <User/>
        </Store2Provider>
    </Store1Provider>;
}

运行报错:

useStore1.ts:9 Uncaught TypeError: Cannot destructure property 'version' of 'useStore2(...)' as it is undefined.
    at useStore1.ts:9:5
    at hox.js?v=d75d37a9:226:22
    at renderWithHooks (react-dom.js?v=d75d37a9:11128:26)
    at updateFunctionComponent (react-dom.js?v=d75d37a9:12953:28)
    at updateSimpleMemoComponent (react-dom.js?v=d75d37a9:12853:18)
    at updateMemoComponent (react-dom.js?v=d75d37a9:12784:22)
    at beginWork (react-dom.js?v=d75d37a9:14092:22)
    at HTMLUnknownElement.callCallback2 (react-dom.js?v=d75d37a9:3738:22)
    at Object.invokeGuardedCallbackDev (react-dom.js?v=d75d37a9:3763:24)
    at invokeGuardedCallback (react-dom.js?v=d75d37a9:3797:39)

如果useStore1与useStore2都是createGlobalStore,则无错。

看了一下源码tests,里面没有这里场景的测试

@freewind
Copy link
Author

似乎没有人关注这个问题

@freewind
Copy link
Author

@awmleer @miracles1919

@codingJJJ
Copy link

createModel corresponds to createGlobalStore in v2, so you need to batch replace createModel with createGlobalStore。see:https://hox.js.org/guide/how-to-migrate-from-v1-to-v2 @freewind

@miracles1919
Copy link
Collaborator

这周我看下 @freewind

@awmleer
Copy link
Member

awmleer commented Sep 26, 2022

@freewind 看你上面发的代码,Store1Provider 是套在 Store2Provider 外层的,那自然不能在 Store1 中去 useStore2 的,违背了依赖的先后顺序

不过 hox 的报错倒是可以优化一下,不能直接 undefined……

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants