删除项目后状态数组变得未定义

charitht99佩雷拉

我是React的新手,目前正在React Redux应用程序上工作,我想在Redux状态下删除数组中的项目。删除一个项目(移动)后,列表在UseSelector中显示为未定义

const mobileList = useSelector((state) => state.MobileReducer.mobileList);

并且由于该映射由于列表未定义而无法正常工作,但是在收到错误后,我检查了Redux开发工具的状态,它显示了列表内的数组而没有删除的项目,并且在添加item,状态更新和清单也显示最新的。

列表组件

import React, { useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import Table from "react-bootstrap/Table";
import Button from "react-bootstrap/Button";
import allActions from "../Actions/index";
import EditModal from "./EditModal";

const MobileList = () => {
  const dispatch = useDispatch();
  debugger;
  const mobileList = useSelector((state) => state.MobileReducer.mobileList);
  const [modalShow, setModalShow] = useState(false);
  const editingItem = useSelector((state) => state.editingItem);
  const [editItem, setEditItem] = useState(false);

  useEffect(() => {
    if(editItem){
      setModalShow(true);
    }else{
      setModalShow(false);
    }
    
  }, [editItem]);

  return (
    <>
      <h1>Available Mobiles</h1>
      <Table responsive>
        <thead>
          <tr>
            <th>Model Name</th>
            <th>Brand Name</th>
            <th>Year</th>
            <th>Price</th>
            <th>Edit</th>
            <th>Delete</th>
          </tr>
        </thead>
        <tbody>
          {mobileList.map((item, i) => {
            return [
              <tr key={i}>
                <td>{item.ModelName}</td>
                <td>{item.Brand}</td>
                <td>{item.Year}</td>
                <td>{item.Price}</td>
                <td>
                  <Button variant="info" onClick={() => setEditItem(item)}>
                    Edit
                  </Button>
                </td>
                <td>
                  <Button
                    variant="danger"
                    onClick={() =>
                      dispatch(allActions.MobileActions.deleteItem(item))
                    }
                  >
                    Delete
                  </Button>{" "}
                </td>
              </tr>,
            ];
          })}
        </tbody>
      </Table>
      {modalShow ? (
        <EditModal
          show={modalShow}
          onHide={() => setModalShow(false)}
          item={editItem}
          onClean={() => setEditItem(null)}
        />
      ) : null}
    </>
  );
};

export default MobileList;

减速器

const initialMobileListState = {
    mobileList:[],
    editingItem:null
}


const counter = (state = initialMobileListState, action) => {
    debugger;
    switch(action.type){
        case "SAVE":
            return {
                ...state,
                mobileList:[...state.mobileList, action.mobile]
            }
        case "EDIT":
            return {
                ...state,
                editingItem:[action.mobile]
            }
        case "DELETE":
            return state.mobileList.filter(a=>a.Id !== action.mobile.Id)
        
        default: 
            return state
    }
}

export default counter
主要

您需要像这样修复Reducer。删除项目时,您需要保留state原始对象结构,就像保存项目时一样。

const initialMobileListState = {
    mobileList:[],
    editingItem:null
}


const counter = (state = initialMobileListState, action) => {
    debugger;
    switch(action.type){
        case "SAVE":
            return {
                ...state,
                mobileList:[...state.mobileList, action.mobile]
            }
        case "EDIT":
            return {
                ...state,
                editingItem:[action.mobile]
            }
        case "DELETE":  // <--------------------------------
            return {
                ...state,
                mobileList: state.mobileList.filter(a=>a.Id !== action.mobile.Id)
            }
        default: 
            return state
    }
}

export default counter

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

设置后 reactjs 状态数组属性未定义

来自分类Dev

为什么函数结束后我的数组变得未定义?

来自分类Dev

在我进行控制台操作后,数组变得未定义。

来自分类Dev

正确的控制台输出后,Javascript数组变得未定义

来自分类Dev

检查未定义后,反应状态未定义

来自分类Dev

数组数组的Vuex状态未定义

来自分类Dev

在表上显示数组时变得未定义

来自分类Dev

尝试删除每个项目时,React Using Hook设置状态未定义

来自分类Dev

推送后未定义的数组

来自分类Dev

从数组中删除未定义的元素

来自分类Dev

未定义:未删除javascript数组

来自分类Dev

从数组中删除未定义的值

来自分类Dev

.Filter()删除未定义的数组

来自分类Dev

从数组中删除未定义的元素

来自分类Dev

数组删除、“未定义”和 JavaScript

来自分类Dev

更新状态后,Redux状态返回未定义

来自分类Dev

AngularJS从数组中删除项目留下未定义的位置代替对象

来自分类Dev

从嵌套数组Ionic3中未定义的firebase中删除项目commig

来自分类Dev

未定义“状态”

来自分类Dev

未定义“状态”

来自分类Dev

调用array_unique后删除未定义的数组索引

来自分类Dev

ReactJS-使用map迭代数组中的每个项目(从状态开始)。项目的属性未定义

来自分类Dev

$ routeParams在angularjs中变得未定义?

来自分类Dev

var在Promise中变得未定义

来自分类Dev

反应-在render()期间“ this”变得未定义

来自分类Dev

更新后Redux状态返回未定义

来自分类Dev

恢复应用程序后,Cordova插件变得未定义

来自分类Dev

删除先前数组时未定义数组

来自分类Dev

从数组中删除元素后迭代数组时发生未定义的偏移量错误

Related 相关文章

热门标签

归档