Search code examples
javascriptredux

i want to declare payload of action but it's failed


i had a trouble to add payload inside an action redux slice reducer,I had search in the internet but I don't find a solution to fix this problem so there's my code reducer:

topicsSlice code:
import {createSlice} from "@reduxjs/toolkit";

const topicsSlice = createSlice({
    name : "topics",
    initialState: {
       topics:{}
    },
    reducers: {
        addTopic: (state, action={type: "topics/addTopic",payload: {id, name, icon}}) => {
      
       const newTopic = {id: id, name: name, icon: icon}
       state.topics = {...state.topics, id: newTopic};
        }
    }

});

export const selectedTopics = (state) => state.topics.topics;
export const {addTopic} = topicsSlice.actions;
export default topicsSlice.reducer;


Solution

  • Destructuring works here like

    addTopic: (state, {payload: {id, name, icon}}) => {