简体   繁体   English

在ReactJS中使用forceUpdate是否比setState()慢?

[英]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.propsthis.state中读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReactJS-setState克隆的对象还是forceUpdate? - ReactJS - setState cloned object or forceUpdate? 在 React 中使用 forceUpdate 而不是 setState? - Using forceUpdate instead of setState in React? 有没有比使用forceUpdate更好的方法 - Is there a better way than using forceUpdate for this 如何修复“index.js:1446 警告:无法在未安装的组件上调用 setState(或 forceUpdate)...”在 ReactJS 中 - How to fix 'index.js:1446 Warning: Can't call setState (or forceUpdate) on an unmounted component..." in ReactJS 为什么在 ReactJS 中使用 Hooks,onClick={forceUpdate} 可以工作? - Why in ReactJS using Hooks, onClick={forceUpdate} can work? React Native:在 FlatList 中调用 setState 比在 ScrollView 中调用 setState 慢得多 - React Native: Calling setState in FlatList much slower than setState in ScrollView 在 React Beautiful 拖放中使用时 Apollo cache.modify 更新比 setState 慢 - Apollo cache.modify update slower than setState while using in React Beautiful Drag and Drop 有没有比forceUpdate()更好的解决方案 - Is there better solution than forceUpdate() 无法使用componentDidMount从API提取数据:无法在已卸载的组件上调用setState(或forceUpdate) - Cannot fetch data from an API using componentDidMount: Can't call setState (or forceUpdate) on an unmounted component ReactJS-将setState与Firebase一起使用 - ReactJS - Using setState with firebase listen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM