DEV Community

Discussion on: React: Controlling Rendering Through Keys

Collapse
 
theoutlander profile image
Nick Karnik

Sorry, I'm not following. Can you give an example of a scenario where you need to force a rerender of children by unmounting them?

Can you use the context API instead or forceUpdate() which actually skips shouldComponentUpdate?

Ideally, every component should be rerendered only if there are changes to the DOM. In this case, since it is a contrived example, I can't wrap my head around a practical use-case. Thanks.

Collapse
 
ganderzz profile image
Dylan Paulus

Agreed, good point Nick. This should be used as a last resort or in special cases.

In my case, we needed to reload the entire page due to a change in where the API lives. Data can come from either a 'global' or 'local' source, and the user has the ability to switch between the two. Most of our API calls live in componentDidMount(), so there is no easy way of re-sending all the page's API calls without a rerender. Something like Redux could of helped, but would cause a huge rewrite.

Collapse
 
pr0da profile image
David Prokaj
Collapse
 
theoutlander profile image
Nick Karnik

I remember reading that, but never needed to use it so probably forgot about it. However, that use-case makes sense.

I generally usee getDerivedStateFromProps because it helps keep the components more efficient.