'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 1:[1]
Destructuring works here like
addTopic: (state, {payload: {id, name, icon}}) => {
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | phry |
