简体   繁体   English

如何在不重新渲染React组件的情况下更新Redux状态

[英]How to update Redux state without re-rendering React component

I have a simple application with two screens: 我有一个带有两个屏幕的简单应用程序:

  • Tasks list screen 任务列表屏幕
  • Task settings screen 任务设置屏幕

On Tasks list screen I have Button so when I click on it: 在“ 任务”列表屏幕上,我具有“ 按钮”,因此当我单击它时:

  1. Dummy task is created in Redux store 虚拟任务在Redux存储中创建
  2. Go to new Task settings screen with passed task_uuid (this screen changes this dummy task making it real / useful) 通过传递的task_uuid转到新的“ 任务设置”屏幕(此屏幕更改了此虚拟任务,使其变得真实/有用)

This is Button press handler 这是按钮按下处理程序

onButtonPress() {
  const task_uuid = this.props.createTask().payload.uuid;
  console.log(`[ON BUTTON PRESS] Task uuid: [${task_uuid}]`);
  this.props.navigation.navigate(
    'TaskSettingsScreen', { task_uuid }
  );
}

And this is dummy task structure (payload property in action creator) 这是虚拟任务结构(动作创建者中的有效负载属性)

export const createTask = () => {
    return {
        type: TASK_CREATED,
        payload:  {
            "uuid": uuidv4(),
            "title": "",
            "tags": [],
            "is_completed": false
        }
    };
};

The reason for this behavior so Task settings don't worry whether task is existing or not (always work with valid task) and can call any property (like tags). 出现这种情况的原因是, 任务设置不必担心任务是否存在(始终与有效任务一起使用),并且可以调用任何属性(如标记)。

Problem: But before navigating to Task settings I see this new dummy task is showed up on list (it uses title property of task and watches Redux state changes). 问题:但是,在导航到“ 任务设置”之前,我看到此新的虚拟任务显示在列表中 (它使用任务的title属性并监视Redux状态更改)。

Question: How can I avoid that problem? 问题:如何避免该问题? So my Redux state changes will affect only new screen (to which I'm going to navigate) and not my current screen? 所以我的Redux状态更改将只影响新屏幕(我要导航至该屏幕),而不影响当前屏幕?

Note: This is a snippet of Tasks list component 注意:这是“ 任务”列表组件的摘要

import React, { Component } from "react";
import { View, FlatList, Text } from "react-native";
import { connect } from "react-redux";
import TaskItem from "./TaskItem";

class TaskList extends Component {
  constructor(props) {
    super(props);
    this.renderItem = this.renderItem.bind(this);
  }

  renderItem({ item }) {
    return <TaskItem task={item} />;
  }

  render() {
    const { tasks } = this.props;
    return (
      <View>
        <FlatList
          data={tasks}
          keyExtractor={task => task.uuid}
          renderItem={this.renderItem}
        />
      </View>
    );
  }
};

const mapStateToProps = state => {
  const { tasks } = state;
  return { tasks };
};

export default connect(mapStateToProps)(TaskList);

The problem here is that you're performing two separate, sequential state changes in response to a single event. 这里的问题是您要响应单个事件执行两个单独的顺序状态更改。

A big advantage of Redux is the ability to perform multiple state changes, simultaneously, in response to a single action. Redux的一大优势是能够响应单个动作同时执行多个状态更改。 (This is what it means to say that actions and reducers are loosely coupled.) (这就是说动作和减速器是松散耦合的。)

I suggest instead of calling navigate separately, to instead have the navigation state change in response to the TASK_CREATED action. 我建议不要单独调用导航,而应根据TASK_CREATED操作更改导航状态。 If your navigation state lives in a Redux reducer, this change should be easy to implement. 如果您的导航状态位于Redux reducer中,则此更改应该易于实现。 If your navigation state lives in a React component, consider how best the change the navigation state in response to a change the Redux store state. 如果您的导航状态位于React组件中,请考虑如何最好地更改导航状态以响应Redux存储状态的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM