Skip to content
Advertisement

React component that’s rendered dynamically does not rerender on parent state changes

I have a component that I want to run through a non react animation library before render. This has prevented me from going the standard route of just using the standard hide/show logic. I initially tried to use ReactDOM.createPortal but that didn’t render the component at all. Using ReactDOM.render, I’ve gotten the element to render correctly upon completion of the animation and I’m able to successfully propagate changes up to the “parent” state but the state change doesn’t propagate back down to the “child”. Here’s my code:

Html

JavaScript

Javascript

JavaScript

Is it possible to do this in react?

Update 1:

So I’ve updated the code per Olivers response, it renders correctly using the portal but the child components still don’t rerender on state changes in the Parent Component

JavaScript

Update 2:

The culprit was found. Changed

JavaScript

to

JavaScript

and it works

Advertisement

Answer

React.createPortal must be used inside the render method (I used a class component because I cannot use hooks in the SO example, you can of course use a functional component).

You can use it in the App component like below or in the Child component :

JavaScript
JavaScript
Advertisement