【问题标题】:TypeError: Cannot read properties of undefined (reading 'type') in redux-toolkitTypeError:无法在 redux-toolkit 中读取未定义的属性(读取 \'type\')
【发布时间】:2023-01-07 02:44:13
【问题描述】:

有人知道我为什么会收到此错误吗? 当我将书籍对象推入状态数组时,会显示此错误。
文件夹结构

这是我的 BookSlice 代码

import { createSlice } from "@reduxjs/toolkit"


const initialState={
    books:[
        {id:"1", name:"Data Structure", author:"motalib"},
        {id:"2", name:"Structure", author:"hossain"},
        {id:"3", name:"Structure", author:"shamim"},
    ]
}

export const bookSlice=createSlice({
    name:"books",
    initialState:initialState,
    reducers:{
        showBook:(state)=>state,
        AddBooks:(state,action)=>{
            state.books.push(action.payload);
        }
    },
})

export const {showBoo, AddBooks}=bookSlice.actions;
export default bookSlice.reducer;

这是我的商店代码

import { configureStore } from "@reduxjs/toolkit";
import bookReducer from "../Features/BookSlice";


const store=configureStore({
    reducer:{
        bookReducer:bookReducer
    }
})
export default store

这是我的调度功能
当我去派遣 add book reducer 时显示此错误

import React, { useState } from "react";
import { useDispatch } from "react-redux";
import AddBooks from "../Features/BookSlice"

const AddBook = () => {
    const dispatch=useDispatch()
    const [book, setBook]=useState({
        id:'',
        name:"",
        author:""
    })

    const HandelChange=(e)=>{
        const {name, value}=e.target
        setBook((prev)=>{
            return{...prev, [name]:value}
        })
    }
    console.log(book);
    const HandleSubmit = (e) => {
        e.preventDefault();
        dispatch(AddBooks(book))
        e.target.reset()
        console.log(book);

    }
return(
here is HTML form
);
};

【问题讨论】:

    标签: javascript reactjs react-redux react-router redux-toolkit


    【解决方案1】:

    这里有错别字

    export const {showBoo, AddBooks}=bookSlice.actions;
    

    它应该是

    export const {showBook, AddBooks}=bookSlice.actions;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-03
      • 2019-04-30
      • 2021-09-30
      • 2021-11-19
      • 2022-01-16
      • 1970-01-01
      • 2021-11-26
      • 2021-11-24
      相关资源
      最近更新 更多