簡體   English   中英

接收狀態突變錯誤

[英]Receiving state mutation error

在我的react / redux應用程序中執行以下操作

export function getSeatingChartConfiguration(team) {
 return function(dispatch) {
  ref.child(team.key).once('value').then(function(snapshot) {
    dispatch(loadSeatingChart(snapshot.val()));
  });
 };
}

export function saveSeatingChartSection(key, sectionData){
 return function(dispatch) {
  ref.child(key).once('value').then(function(snapshot) {
   let data = snapshot.val();
   let sections = data.sections;
   let index = snapshot.val().sections.map( (el) => el.name).indexOf(sectionData.name);
   if(index !== -1) {
    sections[index] = sectionData;
   } else {
    sections.push(sectionData);
   }
   data.sections = sections;
   ref.child(key).update(data, function(error) {
    dispatch(loadSeatingChart(data));
   });
  });
 };
}

這是減速器

export default function seatingChart(state = {}, action) {
 switch(action.type) {

  case actionTypes.LOAD_SEATING_CHART_CONFIGURATION:
   return action.seatingChartConfiguration;

  default:
   return state;
 }
}

調用getSeatingChartConfiguration()時,我沒有收到任何錯誤,但收到Error: A state mutation was detected between dispatches, in the path座位Seatt.sections.2.points Error: A state mutation was detected between dispatches, in the path . 我需要在動作或減速器上進行哪些更改才能不改變狀態。

使用具有初始狀態的Object.assign可以避免如下所示的狀態突變,

const initialState = { seatingChartConfiguration: [] }

export default function seatingChart(state = initialState, action) {
 switch(action.type) {

  case actionTypes.LOAD_SEATING_CHART_CONFIGURATION:
   return Object.assign({}, state, { seatingChartConfiguration: action.seatingChartConfiguration });

  default:
   return state;
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM