简体   繁体   English

React Native-setState和setNativeProps有什么区别?

[英]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. 我在本机应用程序中同时使用setStatesetNativeProps 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 ? setStatesetNativeProps什么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.

相关问题 React setState 和 Hooks setState 有什么区别? - what is the difference between React setState and Hooks setState? React Native 和 React 有什么区别? - What is the difference between React Native and React? react native中componentDidMount和componentDidUpdate有什么区别 - what is difference between componentDidMount and componentDidUpdate in react native setState 中的直接参数和回调有什么区别? - What difference between direct argument and callback in setState? React Native-不同JS文件之间的setState - React Native - setState between different JS file React Native-TextInput.setNativeProps不会触发onChange - React Native - TextInput.setNativeProps isn't triggering onChange react-native 中的 loadingIndicatorSource 和 defaultSource 道具有什么区别? - What is the difference between loadingIndicatorSource and defaultSource props in react-native? 在 React setState 钩子 function 中传递值和传递回调有什么区别? - What's the difference between passing a value and passing a callback in a React setState hook function? Expo和React Native有什么区别? 最新版本2018 - What is the difference between Expo and React Native? latest release 2018 React Native中的alignItems和alignSelf有什么区别? - What's the difference between alignItems and alignSelf in React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM