N
N
nickname133842019-03-06 20:14:23
JavaScript
nickname13384, 2019-03-06 20:14:23

How to force a React class to restart render( )?

How to force react to restart render?
props is received from the parent class
tried using this.forceUpdate() doesn't work or I'm not using it correctly

componentDidUpdate(prevProps, prevState) {
  if(this.props.resetRender === true){
  	console.log(this.props.resetRender) // true
  	this.forceUpdate()
  }
}

gives an error message !
Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Robur, 2019-03-06
@nickname13384

after render, componentDidUpdate is called in which you call render, after which componentDidUpdate is called in which ... I think the idea is clear.
If you need to re-render the component when the parent changes resetRender to true, move this code to 'componentWillUpdate'
In general, the parent should not force the render in this way. If you really want to - make a method and pull it from the parent where you need it directly. or just yank child.forceUpdate(). It will be just as bad, but a little less perversion and will work like clockwork. rerender () { this.forceUpdate() }

A
Alexey Yarkov, 2016-10-27
@Ingword

https://jsfiddle.net/kp047m3b/
=)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question