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

@apollo/client + useSyncExternalStore - data updates not working since 10.9.0+ #3654

Closed
kevineinarsson opened this issue Aug 8, 2022 · 0 comments · Fixed by #3655
Closed

Comments

@kevineinarsson
Copy link

Describe the bug

🙇 my knowledge about anything react 18 is baaad. apologies if the issue is useless. feel free to close if that's the case.

useSyncExternalStore calls in @apollo/client don't update any state. It uses a memoized function (useCallback) as "subscribe" param.

Issues like this one seem to be already known judging by comments in the codebase. But since @apollo/client is a popular package I thought it would be good to open an issue for people like myself...

To Reproduce

I created a minimal test that works when using the method shipped in @apollo/client (link).

In hooks.test.js under the useSyncExternalStore section:

it('works with useCallback', () => {
  let toggle;
  const App = () => {
    const [state, setState] = useState(true);
    toggle = setState.bind(this, () => false);

    const value = useSyncExternalStore(
      useCallback(() => {
        return () => {};
      }, [state]),
      () => state ? 'yep' : 'nope'
    );

    return <p>{value}</p>;
  };

  act(() => {
    render(<App />, scratch);
  });
  expect(scratch.innerHTML).to.equal('<p>yep</p>');

  toggle();
  rerender();

  expect(scratch.innerHTML).to.equal('<p>nope</p>');
});

When replacing useSyncExternalStore with the function that's shipped with @apollo/client, the value updates properly.

Workarounds
Using patch-package (😄) to null out the useSyncExternalStore import from React works, leading to the shipped method in the library to be used.

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

Successfully merging a pull request may close this issue.

1 participant