【问题标题】:When do i really need redux in react native?我什么时候真的需要 redux 来反应原生?
【发布时间】:2018-05-30 22:49:34
【问题描述】:

我开始学习 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 不再按预期工作

标签: react-native redux state-management


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    在我看来,您应该为简单的应用程序使用本地状态。和 Redux for business app,按时间扩展功能。

    顺便说一句,如果组件太复杂,你应该把它分解成一些小组件以便重用并控制状态如何工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多