Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Alternative to forceUpdate #55

Closed
arahansen opened this issue May 16, 2016 · 5 comments
Closed

Alternative to forceUpdate #55

arahansen opened this issue May 16, 2016 · 5 comments

Comments

@arahansen
Copy link

React docs warn against using forceUpdate to update a component because it is not as efficient.
https://facebook.github.io/react/docs/component-api.html#forceupdate

mobx-react is using this API to react to observable changes: https://github.com/mobxjs/mobx-react/blob/master/index.js#L53

Would it be worth exploring alternatives to forceUpdate, such as forcing rerendering through something like setState?

@andykog
Copy link
Member

andykog commented May 16, 2016

@arahansen, this.setState({}) would indeed result in rerendering, but then there is no guarantee, that it happens synchronously, so that mobx is able to derive reactive values, used during rendering.

@mweststrate
Copy link
Member

indeed, forceUpdate is used internally, but it is behind MobX own scheduling mechanism, which is even more optimal then Reacts own setState mechanism. So as a component developer you shouldn't use it, but it is a perfect low level primitive where libs like mobx-react can build upon

@arahansen
Copy link
Author

Interesting, I was not aware of this!

Do you have any related studies or information on how forceUpdate is a preferred primitive for building libraries?

@mweststrate
Copy link
Member

No, just testing / reading sources / reverse engineering. It is just that setState, schedules, where forceUpdate executes. Sinces MobX already schedules, it is the better fit that avoids doing similar administration twice and avoids triggering unrelated lifecycle hooks. I hope that clarifies!

@arahansen
Copy link
Author

Yes, thanks for the explanation!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants