C
C
Coder3212017-03-01 22:54:12
React
Coder321, 2017-03-01 22:54:12

What is the difference between forceUpdate and setState?

What is the difference between forceUpdate and setState? Can using forceUpdate lead to some side effects?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan, 2017-03-01
@Coder321

Seriously?!
of course it can. But in general, this is rather an exception to the rule and it is better to use setState ()
In short:
setState () writes data to this. state and causes rendering.
forceUpdate() does nothing, it just triggers a render. Basically a useless function, i.e. if neither props nor state have changed, then one fig will be rendered, the same thing. To be honest, it doesn’t even occur to me why this might be needed, except for some magical asynchronous crutches in render .

D
dollaween, 2019-04-05
@dollaween

forceUpdate may be needed when working with localStorage, whose data does not make sense to duplicate in state (if this data is only rendered and not sent to the server, of course)

R
Rinat Garifullin, 2019-11-29
@rinat95

the only time forceUpdate was needed was to implement media queries on a project that needed ie8 support.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question