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

Adding Items doesn't work properly when using create-react-app #27

Open
Dianoga opened this issue Apr 27, 2020 · 10 comments
Open

Adding Items doesn't work properly when using create-react-app #27

Dianoga opened this issue Apr 27, 2020 · 10 comments
Labels
help wanted 👋 Extra attention is needed

Comments

@Dianoga
Copy link

Dianoga commented Apr 27, 2020

Minimal reproduction repo: https://github.com/Dianoga/muuri-react-cra-bug

There seems to be an issue in apps created using create-react-app.

Specifically, when you add new items to the MuuriComponent they don't seem to get initialized properly.

In the screenshot below you can see the first three items that were created on the first render of MuuriComponent. The second set of three items were added to the state and render, but don't get the muuri properties.
image

Additionally, when navigating away from the page with the MuuriComponent and the broken items the below error shows up. That isn't demonstrated in the reproduction repo.
image

Interestingly, when you build the project (using npm run build) it behaves correctly.

I did a first pass at trying to figure out what was going on, but this is a complicated codebase and I pretty rapidly got lost.

@Dianoga
Copy link
Author

Dianoga commented Apr 27, 2020

I'm still digging but it looks like the new Item is being rendered properly, but before the useEffect stuff fires it's being rendered again which clears all the vars.

Also, the same issue occurs if the MuuriComponent initially renders without children and then renders again with children added.

@Dianoga
Copy link
Author

Dianoga commented Apr 27, 2020

I've been trying to come up with a way around the problem. My current idea is to keep track of the keys that have been actually been added and check the key of each child against that, but I don't have that working yet.

@paol-imi
Copy link
Owner

paol-imi commented Apr 27, 2020

Hi @Dianoga

Thanks for the report, I will investigate thoroughly.

Now I have tried to reproduce the problem on codesandox, using the create-react-app sandbox and using the grid demo code.

The app seems to work, I noticed that the code was mistakenly changed.

this

<Item key={id} />

was changed to this

<Item key={"id"} />

And obviously made the app break. Could you try to reproduce the problem by copying the grid demo code?

@Dianoga
Copy link
Author

Dianoga commented Apr 27, 2020

I checked out your sandbox. One difference I saw from the normal create-react-app is not using React.StrictMode in the render call. When I add that to your sand box the behavior lines up with what I see.

ReactDom.render(<React.StrictMode><App /></React.StrictMode>, document.getElementById("root"));

StrictMode isn't reporting any problems, but I'm wondering if the extra checks it does slows things down enough that it's exposing a race condition.

From https://reactjs.org/docs/strict-mode.html

Strict mode can’t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:

  • Class component constructor, render, and shouldComponentUpdate methods
  • Class component static getDerivedStateFromProps method
  • Function component bodies
  • State updater functions (the first argument to setState)
  • Functions passed to useState, useMemo, or useReducer

@paol-imi
Copy link
Owner

It is strange behavior, I will investigate further.

I didn't understand if you managed to reproduce the bug with Strict mode.

@Dianoga
Copy link
Author

Dianoga commented Apr 27, 2020

With StrictMode added to your sandbox the bug occurs.

@paol-imi paol-imi added the help wanted 👋 Extra attention is needed label May 14, 2020
@kongweiying2
Copy link

kongweiying2 commented Nov 26, 2020

I am also having this problem. It's a create react app using typescript. React 17 generated the app, but I downgraded to React 16 which didn't solve the issue.

EDIT: After applying #43, this fixed my issue. So I downgraded to React 16.13, and it worked even in Strict Mode.

@Secretmapper
Copy link

Fixed this with https://github.com/Secretmapper/muuri-react/tree/strict-muuri-0.9 - properly adding the old children using useEffect

@Mohamed-Raid
Copy link

Secretmapper

can you please explain i have the same issue here

@Mohamed-Raid
Copy link

sandbox

sandbox example does not exist :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted 👋 Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants