admin管理员组文章数量:1410705
I am trying to switch an app I am building over to use Redux Toolkit, and have noticed this error ing up 'A non-serializable value was detected in an action, in the path: type
. Value: ', Also getting undefined in the redux devtool instead of expected auth/registerFail, the state is changing as expected.
import axios from 'axios';
import { setAlert } from '../alerts/alertSlice';
const slice = createSlice({
name: 'auth',
initialState: {
token: localStorage.getItem('token'),
isAuthenticated: null,
loading: true,
user: null,
},
reducers: {
registerSuccess: (state, action) => {
const { payload } = action.payload;
state.push({
payload,
isAuthenticated: true,
loading: false,
});
},
registerFail: (state) => {
localStorage.removeItem('token');
state.token = null;
state.isAuthenticated = false;
state.loading = false;
state.user = null;
},
},
});
const { registerSuccess, registerFail } = slice.actions;
export default slice.reducer;
// Register User
export const register =
({ name, email, password }) =>
async (dispatch) => {
const config = {
headers: {
'tent-Type': 'application/json',
},
};
const body = JSON.stringify({ name, email, password });
try {
const res = await axios.post('/api/users', body, config);
dispatch({
type: registerSuccess,
payload: res.data,
});
} catch (err) {
const errors = err.response.data.errors;
if (errors) {
errors.forEach((error) => dispatch(setAlert(error.msg, 'danger')));
}
dispatch({
type: registerFail
});
}
};
I am trying to switch an app I am building over to use Redux Toolkit, and have noticed this error ing up 'A non-serializable value was detected in an action, in the path: type
. Value: ', Also getting undefined in the redux devtool instead of expected auth/registerFail, the state is changing as expected.
import axios from 'axios';
import { setAlert } from '../alerts/alertSlice';
const slice = createSlice({
name: 'auth',
initialState: {
token: localStorage.getItem('token'),
isAuthenticated: null,
loading: true,
user: null,
},
reducers: {
registerSuccess: (state, action) => {
const { payload } = action.payload;
state.push({
payload,
isAuthenticated: true,
loading: false,
});
},
registerFail: (state) => {
localStorage.removeItem('token');
state.token = null;
state.isAuthenticated = false;
state.loading = false;
state.user = null;
},
},
});
const { registerSuccess, registerFail } = slice.actions;
export default slice.reducer;
// Register User
export const register =
({ name, email, password }) =>
async (dispatch) => {
const config = {
headers: {
'tent-Type': 'application/json',
},
};
const body = JSON.stringify({ name, email, password });
try {
const res = await axios.post('/api/users', body, config);
dispatch({
type: registerSuccess,
payload: res.data,
});
} catch (err) {
const errors = err.response.data.errors;
if (errors) {
errors.forEach((error) => dispatch(setAlert(error.msg, 'danger')));
}
dispatch({
type: registerFail
});
}
};
Share
Improve this question
asked Aug 31, 2021 at 12:02
RickstaRicksta
271 silver badge4 bronze badges
1 Answer
Reset to default 4You are accidentally using the action creator functions as types here.
So instead of
dispatch({
type: registerSuccess,
payload: res.data,
});
do this:
dispatch(registerSuccess(res.data));
本文标签:
版权声明:本文标题:javascript - redux-toolkit-> 'A non-serializable value was detected in an action' while converting workin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744999755a2636903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论