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

useListContext() causes infinite render loops #5974

Closed
stensrud opened this issue Mar 1, 2021 · 1 comment
Closed

useListContext() causes infinite render loops #5974

stensrud opened this issue Mar 1, 2021 · 1 comment

Comments

@stensrud
Copy link
Contributor

stensrud commented Mar 1, 2021

What you were expecting:
When using useListContext(), I expect the returned values to be the same object instances when the values are the same.

What happened instead:
Upgrading ra-core to version 3.12.4 from version earlier than 3.12.3 (3.8.3) broke our production system. We tracked it down to this pull request: #5802

To my understanding, in React applications, one should adhere to strict immutable practices when passing props or state around: Never return the same object if the content has changed, and always return the same object if nothing has changed. This ensures very fast comparison of old and new values, and thus very fast rendering.

It seems, useListContext() started breaking these practices by never returning the same object for selectedIds.

Consider this example code:

const { selectedIds } = useListContext();

useEffect(() => {
  // trigger new render by updating state here
}, [selectedIds]);

This will easily result in infinite render loops, crashing the application.

Our ugly fix: JSON.stringify(selectedIds) !== JSON.stringify(oldSelectedIds)

Steps to reproduce:

I added an InfiniteLoop-component to PostList.
https://codesandbox.io/s/vigorous-currying-2vnyn?file=/src/posts/PostList.tsx

Not sure if it is demonstrating the problem correctly. Also I'm completely open to this is a misunderstanding on my part.

Environment

  • React-admin version: 3.12.4
  • Last version that did not exhibit the issue (if applicable): 3.12.2
  • React version: 16.9.0
  • Browser: Chrome, Safari
@djhi
Copy link
Contributor

djhi commented Mar 1, 2021

Hi and thanks for the report. There are many things happening on the codesandbox, including two conflicting versions of MUI and too much code non related to the issue.

I tried to reproduce the issue by adding the InfiniteLoop component to the simple example locally. Although it does run the effect multiple times, there is no infinite loop. The fact that it run the effects so many times is because how contexts work. It's a known issue in React and we can't do anything about it. See reactjs/rfcs#119 for more details.

Please reopen this if you can reproduce the issue in a minimal example.

@djhi djhi closed this as completed Mar 1, 2021
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

2 participants