简体   繁体   English

mapStateToProps在更改时不更新组件

[英]mapStateToProps not updating component on change

New to react/redux. 反应/还原的新功能。 Wondering why my component is not refreshing on the fired action. 想知道为什么我的组件没有在触发的动作上刷新。 Also not sure whether reducer is a good place to manipulate the store but it is pure so it should work. 也不确定减速器是否是操纵商店的好地方,但是它是纯净的,因此应该可以使用。

What happens is that action is fired and store is updated. 发生的情况是触发了操作并更新了存储。 But the clicked item is not updating (props update shoud add class added to it) 但点击项目不更新(更新道具增加768,16类added到它)

Component: 零件:

Referencing rcm_data by Id to toggle class added , onClick fire action 通过ID引用rcm_data来切换已added类,onClick触发操作

import React, { Component } from "react";
import { connect } from "react-redux";
import { addRateCard } from "../actions";

import "./RcmEditor.css";

class RcmEditor extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <table className="slds-table slds-table--cell-buffer slds-table--bordered">
        <thead>
          <tr>
            <th>Rate Card Name</th>
          </tr>
        </thead>
        <tbody>
          {group.rateCardIds.map(rcId => {
            return (
              <tr
                className={
                  "ed-group-rc " +
                  (this.props.rcm_data[rcId].added ? "added" : "")
                }
                key={rcId}
                onClick={() =>
                  this.props.addRateCard(this.props.rcm_data[rcId])
                }
              >
                <td colSpan="100">{this.props.rcm_data[rcId].Name}</td>
              </tr>
            );
          })}
        </tbody>
      </table>
    );
  }
}

const mapDispatchToProps = {
  addRateCard
};

const mapStateToProps = state => {
  return { rcm_data: state.rcm_data, group_data: state.group_data };
};

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(RcmEditor);

Actions: 操作:

const ADD_RATE_CARD = "ADD_RATE_CARD";

export const addRateCard = rateCard => ({ type: ADD_RATE_CARD, payload: rateCard });

Reducer: 减速器:

Copy state (just a precaution), increment added_count property of group_data and change added property of rcm_data referenced by action. 复制状态(为防患于未然),递增group_data的add_count属性,并更改操作引用的rcm_data的add属性。 payload Id, return a new state. 有效负载ID,返回新状态。 Everything seems pure here 这里的一切似乎都是纯净的

const initialState = {
    rcm_data: {},
    group_data: {},
};

const rootReducer = (state = initialState, action) => {
    case "ADD_RATE_CARD":

        let _state = { ...state };
        let rateCard = action.payload;

        _state.group_data[rateCard.GroupName].added_count++;
        _state.rcm_data[rateCard.Id].added = true;

        let data = {
            rcm_data: _state.rcm_data,
            group_data: _state.group_data
        };

        return { ...state, rcm_data: _state.rcm_data, group_data: _state.group_data };
    default:
        return state;
   }
};

export default rootReducer;

Your reducer copies objects ( rcm_data and group_data ) and mutates them. reducer复制对象( rcm_datagroup_data )并对其进行突变。

Redux compares shallowly - using the same object refs there is no difference then no update/rerender. Redux比较浅-使用相同的对象引用,没有区别,就没有更新/渲染。

Simply create a new sub-objects, sth like: 只需创建一个新的子对象,例如:

    let data = {
        rcm_data: {...state.rcm_data},
        group_data: {...state.group_data}
    };
    data.group_data[rateCard.GroupName].added_count++;
    data.rcm_data[rateCard.Id].added = true;
    return data

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

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