gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:16:39 25 4
gpt4 key购买 nike

我想防止整个树的重新渲染,所以,我想在需要时使用 setNativeProps 来更新特定组件,但是 setNativeProps 不起作用对于所有组件。我在我的 React Native 应用程序中同时使用了 setStatesetNativePropssetState 适用于所有组件,但 setNativeProps 不适用于所有组件。

setStatesetNativeProps 有什么区别?对于什么样的组件应该使用和不应该使用setNativeProps?一个小例子将更受欢迎。谢谢!!!

最佳答案

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. setNativeProps is the React Native equivalent to setting properties directly on a DOM node.

检查 this link 供引用

Use setNativeProps when frequent re-rendering creates a performance bottleneck

所以基本上我能看到的唯一用例是当您创建连续动画并且不想影响应用程序的性能时。在几乎所有其他情况下,setState 就足够了。如果您需要控制组件何时重新渲染,请查看 shouldComponentUpdate

关于javascript - React Native - setState 和 setNativeProps 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53477522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com