通过 Redux 的化简器更新列表



我正在制作一个小食谱列表,用于学习React(因为我是新手)。

我能够弄清楚如何从列表中添加和删除食谱。然而,我很难从列表(应用程序的状态)中编辑食谱。

来自Redux的文档:

因为我们想要更新数组中的特定项通过突变,我们必须创建一个具有相同元素的新数组除索引项外的项

我有问题如何选择特定项目在数组中,当被选择的数组本身被修改。

这是我的Actions文件:

<<p> src/行动/strong>
export const RECIPE_ADD = 'RECIPE_ADD';
export const RECIPE_EDIT = 'RECIPE_EDIT';
export const RECIPE_DELETE = 'RECIPE_DELETE';
export function addRecipe(recipe) {
  return {
    type: RECIPE_ADD,
    payload: recipe
  }
}
export function editRecipe(recipe) {
  return {
    type: RECIPE_EDIT,
    payload: recipe
  }
}
export function deleteRecipe(recipe) {
  return {
    type: RECIPE_DELETE,
    payload: recipe
  }
}

src/还原剂reducer_recipe

import { RECIPE_ADD } from '../actions/index';
import { RECIPE_DELETE } from '../actions/index';
import { RECIPE_EDIT } from '../actions/index'
const defaultList = [
  { recipe: 'Pizza', ingredients: ['tomato-sauce','cheese','peperoni'] },
  { recipe: 'Pie', ingredients: ['dough','cherry'] },
  { recipe: 'Curry', ingredients: ['rice','sauce','carrots'] },
];
export default function(state = defaultList, action){
  switch (action.type) {
    case RECIPE_ADD:
      return [
        { recipe: action.payload[0], ingredients: action.payload[1] },
        ...state
      ];
    case RECIPE_DELETE:
    let index = state.map(x => x.recipe).indexOf(action.payload.recipe)
      return (
        state.slice(0,index).concat(state.slice(index + 1))
      )
    case RECIPE_EDIT:
    console.log(action.payload)
    // action.payload is the updated selected recipe
      return (
        state
      )
  }
  return state;
}

我怀疑我需要在动作中添加一个id来区分它与数组列表?

您应该为defaultList中的对象添加id:

const defaultList = [
  { id: 1, recipe: 'Pizza', ingredients: ['tomato-sauce','cheese','peperoni'] },
  { id: 2, recipe: 'Pie', ingredients: ['dough','cherry'] },
  { id: 3, recipe: 'Curry', ingredients: ['rice','sauce','carrots'] },
];

然后更新你的食谱:

    case RECIPE_EDIT:
      return state.map((recipe)=> {
        if( recipe.id == action.payload.id ) {
          return action.payload
        } else {
          return recipe;
        }
      });

只有当你确定recipe.idaction.payload.id都是整数时,你才应该使用===而不是==

可能是这样的工作:

import { RECIPE_ADD } from '../actions/index';
import { RECIPE_DELETE } from '../actions/index';
import { RECIPE_EDIT } from '../actions/index'
const defaultList = [
  { recipe: 'Pizza', ingredients: ['tomato-sauce','cheese','peperoni'] },
  { recipe: 'Pie', ingredients: ['dough','cherry'] },
  { recipe: 'Curry', ingredients: ['rice','sauce','carrots'] },
];
export default function(state = defaultList, action){
  switch (action.type) {
    case RECIPE_ADD:
      return [
        { recipe: action.payload[0], ingredients: action.payload[1] },
        ...state
      ];
    case RECIPE_DELETE:
      let index = state.map(x => x.recipe).indexOf(action.payload.recipe)
        return (
          state.slice(0,index).concat(state.slice(index + 1))
        )
    case RECIPE_EDIT:
      return state.map((recipe)=> {
        if( recipe.name === action.payload.name ) {
          return action.payload
        } else {
          return recipe;
        }
        return recipe;
      });
  }
  return state;
}

相关内容

  • 没有找到相关文章

最新更新