gpt4 book ai didi

react-native - 我什么时候真正需要在 React Native 中使用 redux?

转载 作者:行者123 更新时间:2023-12-02 14:59:51 26 4
gpt4 key购买 nike

我开始学习 react-native 和 redux。在某些领域,由于复杂性,我可以在某些组件中使用 redux,而某些组件只是通过组件中的 setState 和 this.state 在 react-native 中使用本地状态。

    import React, { Component } from 'react';
import { AppRegistry, Text, View } from 'react-native';

class Blink extends Component {
constructor(props) {
super(props);
this.state = {isShowingText: true};

// Toggle the state every second
setInterval(() => {
this.setState(previousState => {
return { isShowingText: !previousState.isShowingText };
});
}, 1000);
}

render() {
let display = this.state.isShowingText ? this.props.text : ' ';
return (
<Text>{display}</Text>
);
}
}

最佳答案

作为一个过于简单化的经验法则,我会说使用 Redux 存储与不同的不相关组件相关的数据,使用组件状态存储在组件及其父或子组件之外没有意义的数据。

Redux 基本上是一个内存数据存储,如果您真的不需要,它会向您的应用程序添加大量样板代码。

关于react-native - 我什么时候真正需要在 React Native 中使用 redux?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50614337/

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