[英]React Native - What is the difference between setState & setNativeProps?
I want to prevent re-rendering of the whole tree, so, I thought to use the setNativeProps
to update the specific component when needed, but setNativeProps
is not working for all components. 我想防止重新渲染整个树,因此,我想在需要时使用setNativeProps
更新特定组件,但setNativeProps
不适用于所有组件。 I am using both setState
and setNativeProps
in my react native application. 我在本机应用程序中同时使用setState
和setNativeProps
。 The setState
works just fine for all components but setNativeProps
do not works for all components. setState
适用于所有组件,但setNativeProps
不适用于所有组件。
What is the difference between setState
& setNativeProps
? setState
和setNativeProps
什么setNativeProps
? For what kind of components setNativeProps
should and shouldn't be used? setNativeProps
应该和不应该用于哪种组件? A little example will be more appreciated. 一个小例子将不胜感激。 Thanks !!! 谢谢 !!!
The React-Native Documentation explains this very well : React-Native文档对此进行了很好的解释:
It is sometimes necessary to make changes directly to a component without using state/props to trigger a re-render of the entire subtree. 有时有必要直接对组件进行更改,而无需使用状态/属性来触发整个子树的重新渲染。 When using React in the browser for example, you sometimes need to directly modify a DOM node, and the same is true for views in mobile apps. 例如,在浏览器中使用React时,有时需要直接修改DOM节点,移动应用程序中的视图也是如此。 setNativeProps is the React Native equivalent to setting properties directly on a DOM node. setNativeProps是React Native,等效于直接在DOM节点上设置属性。
check this link for reference 检查此链接以供参考
Use setNativeProps when frequent re-rendering creates a performance bottleneck 频繁重新渲染会产生性能瓶颈时,请使用setNativeProps
so basically the only use case i can see for it, is when you are creating continuous animations and you don't want to affect the performance of your app. 因此,基本上,我能看到的唯一用例是创建连续动画且不希望影响应用程序性能时。 in almost all other cases, setState will be more than enough. 在几乎所有其他情况下,setState绰绰有余。 And in case you need to control when your component should re-render check out shouldComponentUpdate 并且如果您需要控制何时应重新渲染组件,请检查shouldComponentUpdate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.