The issue is that Redux dispatches the updated state first to the parent, then to the child (ElementsContainer). Whenever I add an element to the state, the refs are assigned only after useEffect (which is meant to trigger some DOM animations with the GSAP library leveraging the ref) has been executed, meaning that useEffect cannot find the ref yet.
So far this is how I solved:
I added an early return inside useEffect in case the ref doesn't exist
I created a state in the parent that keeps count of the elements manipulation
I pass down the setter of the state to the element, triggering it inside the ref callback
I added the state that keeps count as a dependency of useEffect
All of this causes two renders for each manipulation of the elements list. I really dislike this solution and I wonder whether you can suggest a better pattern.
I made a PoC of the app using Context instead of Redux and it works as intended (here you can find the fiddle), but I still would like to hear suggestions.
This isn't a Redux core question - it's more related to React-Redux.
We try to keep the issues list focused on just actual bugs and feature requests, not usage discussions. I'd suggest opening a StackOverflow issue about this, or asking over in the Reactiflux chat channels.
It does seem like a complicated use case, and I don't think we have any real suggestions here.
I have a container populated dynamically with elements based on an array in the Redux store. A code example follows.
The issue is that Redux dispatches the updated state first to the parent, then to the child (
ElementsContainer
). Whenever I add an element to the state, the refs are assigned only afteruseEffect
(which is meant to trigger some DOM animations with the GSAP library leveraging the ref) has been executed, meaning that useEffect cannot find the ref yet.So far this is how I solved:
useEffect
in case the ref doesn't existAll of this causes two renders for each manipulation of the elements list. I really dislike this solution and I wonder whether you can suggest a better pattern.
I made a PoC of the app using Context instead of Redux and it works as intended (here you can find the fiddle), but I still would like to hear suggestions.