This might be a stupid question but I have some useEffects that act as initial triggers and set up the component. Now, I could trigger all these useEffects with some flag in the dependancy array and kinda fake a "reconstruction" of the component but is there a way to do it properly?
So that the component rerenders as if its doing it for the first time? Thanks for any suggestions...
My use case is as follows: A generic React component that you provide an ID, on initial rendering it will check redux store if there is a state existing for this id, if yes, it grabs it and renders the state from store. Additionally, it renders third party components that behave in a certain way given the initial properties you pass it.
So, I basicly rely on reacts way of only triggering a useEffect once. But how would I control react to do it again, from outside...
This is not a duplicate because I dont want to force an update, I need to emulate a complete initial construction of the component.
The reason for this is because down the line I render third party components like so:
useEffect(()=>{...},[]); // initial set up
if(something){
return <SomethirdPartyComponentsThatBehaveWellWhenInitialized />;
}
When something is true the first time, the third party components initialize perfectly and everything works fine.
However the issue is when I set something to false and then back to true again.
Now, my props that I pass to the third parties are all set up to act as if something has just become true, therefore, they are the initial props for the third party components and are handled this way.
However the third party components are already rendered and now behave out of sync because theyre still built off the old initialization, no longer corresponding to the new props...
The easiest fix in my head: Tell react that at this point you want <SomethirdPartyComponentsThatBehaveWellWhenInitialized />;
to be reconstructed as if it were the first time.
This would also recursively trigger that initialization in child components which would also be super helpful.