Skip to content

[Regression Bug] Updated atom value not propagated to component #947

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

Closed
xbaun opened this issue Jan 10, 2022 · 10 comments · Fixed by #970 or #960
Closed

[Regression Bug] Updated atom value not propagated to component #947

xbaun opened this issue Jan 10, 2022 · 10 comments · Fixed by #970 or #960
Assignees
Labels
bug Something isn't working

Comments

@xbaun
Copy link
Contributor

xbaun commented Jan 10, 2022

I am using an atom (A) to control if an atom (B) should return the value of an atom (C) otherwise a default value is returned. See the example below: https://codesandbox.io/s/dark-http-bsili?file=/src/App.tsx

const aAtom = atom<any>(false);
aAtom.debugLabel = "aAtom";
aAtom.onMount = (set) => {
    console.log("onMount: aAtom");
    setTimeout(() => {
        set(true);
    }, 1000);
};

const cAtom = atom<any>(1);
cAtom.debugLabel = "cAtom";
cAtom.onMount = (set) => {
    console.log("onMount: cAtom");
    set(2);
};

const bAtom = atom<any>((get) => {
    const a = get(aAtom);
    console.log("a=", a);
    if (a) {
        return get(cAtom);
    }
    return 1;
});
bAtom.debugLabel = "bAtom";

The problem is, that the changed value of atom (C) is never propagated to the invoking component through useAtom. The error occurs when there is no externally triggered rendering within the calling component, which re-evaluates the useAtom hook. I think, there is somewhere a flushPending missing. I tried to figure out what could be a possible fix, but I did not found any good solution.

Affects version 1.5.0. Worked with 1.4.2 1.4.9.


BTW: I think the useEffect call at https://github.com/pmndrs/jotai/blob/main/src/core/useAtom.ts#L103-L105 should pass its dependencies. If so, a forced rendering has no effect anymore on the bug above.

  useEffect(() => {
    store[COMMIT_ATOM](atom, version)
  }, [store, atom, version]);
@dai-shi
Copy link
Member

dai-shi commented Jan 11, 2022

Thanks for reporting. I think we miss tests to cover this...

Affects version 1.5.0. Worked with 1.4.2.

This almost indicates, this is a bug. Note: it works with 1.4.9 too.

@dai-shi dai-shi added the bug Something isn't working label Jan 11, 2022
@xbaun
Copy link
Contributor Author

xbaun commented Jan 11, 2022

I updated the version in the bug description.

@smashercosmo
Copy link

Hm... I think I also stumbled upon this bug, but for me the last working version is 1.4.6

@dai-shi
Copy link
Member

dai-shi commented Jan 14, 2022

Are you sure? That sounds like a different bug. Thanks for reporting anyway.
I'm currently working hard on issue.

@dai-shi dai-shi self-assigned this Jan 14, 2022
@smashercosmo
Copy link

Yeah, maybe different bug, but same behaviour: updated atom value is not reflected in a component. Works in 1.4.6, doesn't work in all other versions > 1.4.6. I'll try to create reproduction sandbox.

@dai-shi
Copy link
Member

dai-shi commented Jan 15, 2022

Would you guys try if https://ci.codesandbox.io/status/pmndrs/jotai/pr/960/builds/208213 fixes issues? (See "Local Install Instructions")

@xbaun-mms
Copy link

Couldn't test it with my application because it breaks currently because of #967. But as far es I can see it, the test is working and increasing delays also works perfectly.

@dai-shi
Copy link
Member

dai-shi commented Jan 15, 2022

See https://ci.codesandbox.io/status/pmndrs/jotai/pr/960 for new builds.

@dai-shi
Copy link
Member

dai-shi commented Jan 16, 2022

doesn't fix

@dai-shi dai-shi reopened this Jan 16, 2022
@dai-shi
Copy link
Member

dai-shi commented Jan 16, 2022

#960 should fix this.
Please try: https://ci.codesandbox.io/status/pmndrs/jotai/pr/960

dai-shi added a commit that referenced this issue Jan 18, 2022
…ing atom value (#960)

* fix: add test case for #947

* wip: partial fix

* wip: made a progress

* fix test!

* handle dependencies change without value change

Co-authored-by: daishi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants