I'm getting this error Unhandled Rejection (TypeError): state.push is not a function while using redux thunk but while refrshing the page after error, new word is getting added to the DB.
Below is my code.
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
export const getDictionaryAsync = createAsyncThunk(
"dictionary/getDictAsync",
async () => {
let res = await fetch("https://vocabulary-app-be.herokuapp.com/dictionary");
if (res.ok) {
let dictData = await res.json();
return { dictData };
}
}
);
export const addWordtoDictAsync = createAsyncThunk(
"dictionary/addWordtoDictAsync",
async (payload) => {
let res = await fetch(
"https://vocabulary-app-be.herokuapp.com/dictionary",
{
method: "POST",
body: JSON.stringify({ word: payload.word }),
headers: {
"Content-Type": "application/json",
},
}
);
if (res.ok) {
let data = await res.json();
console.log(data);
return { data };
}
}
);
const dictionarySlice = createSlice({
name: "dictionary",
initialState: [],
reducers: {},
extraReducers: {
[getDictionaryAsync.fulfilled]: (state, action) => {
return action.payload.dictData;
},
[addWordtoDictAsync.fulfilled]: (state, action) => {
console.log(action.payload.data + "reducer");
state.push(action.payload.data);
},
},
});
export default dictionarySlice.reducer;