[英]Is using forceUpdate slower than setState() in ReactJS?
I'm experimenting with building a ReactJS app that doesn't use ReactJS components state or props for display data. 我正在尝试构建一个不使用ReactJS组件状态或道具来显示数据的ReactJS应用。 Instead I use props for passing models to the component so that it will bind
self
to it, and get re-rendered on model data change. 取而代之的是,我使用道具将模型传递给组件,以便它将
self
绑定到组件,并在模型数据更改时重新呈现。
I would like to keep the component state 'clean' in such way that the component's state would only contain data about user experience related stuff. 我想以使组件状态仅包含有关用户体验相关内容的数据的方式保持组件状态“干净”。 If you are familiar with Firebase, the apps works very much like when building a react app using Firebase, but it should be more declarative.
如果您熟悉Firebase,则这些应用程序的工作原理与使用Firebase构建React应用程序时非常相似,但应更具声明性。
Because I do not use component.setState({...})
I would like to use component.forceUpdate
. 因为我不使用
component.setState({...})
所以我想使用component.forceUpdate
。 I have read many times that you shouldn't use this method because it skips the lifecycle methods, but this is an experiment. 我已经读过很多次了,您不应该使用此方法,因为它会跳过生命周期方法,但这是一个实验。
What I'm mostly interested in right now is whether it is slower than setState
and does ReactJS do the same smart DOM diffing when an update is forced? 我现在最感兴趣的是它是否比
setState
慢,并且在强制更新时ReactJS是否执行相同的智能DOM区分?
React's forceUpdate()
is not slower than setState()
. React的
forceUpdate()
不比setState()
慢。
It uses the same smart DOM "diffing" algorithm , so that component updates are predictable while being fast enough for high-performance apps. 它使用相同的智能DOM“差异”算法 ,因此组件更新是可预测的,同时对于高性能应用程序来说足够快。
Calling forceUpdate()
will cause render()
to be called on the component, skipping shouldComponentUpdate()
. 调用
forceUpdate()
将导致在组件上调用render()
,而跳过shouldComponentUpdate()
。 This will trigger the normal lifecycle methods for child components, including the shouldComponentUpdate()
method of each child. 这将触发子组件的正常生命周期方法,包括每个子组件的
shouldComponentUpdate()
方法。 React will still only update the DOM if the markup changes. 如果标记发生更改,React仍然只会更新DOM。
PS: Yes, normally you should try to avoid all uses of forceUpdate()
and only read from this.props
and this.state
in the Component's render()
method. PS:是的,通常应该避免使用
forceUpdate()
而只能从Component的render()
方法中的this.props
和this.state
中读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.