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

When do i really need redux in react native?

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

    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>
    );
  }
}

您应该阅读 You Might Not Need Redux 以了解我们为什么要使用 Redux。本文由 Redux 作者撰写。

然后,您就可以在您的组件中使用本地状态和 redux 存储。

在我看来,您应该为简单的应用程序使用本地状态。以及用于商业应用程序的 Redux,按时间扩展功能。

顺便说一句,如果组件太复杂,您应该将其分解成一些小组件以便重用并控制状态的工作方式。

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

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