[英]componentwillmount() Uncaught Error: Actions must be plain objects. Use custom middleware for async actions
I'm implementing get all image by type with redux-saga.我正在使用 redux-saga 实现按类型获取所有图像。 I have 2 types, let's say, type kristik and type motif.
我有 2 种类型,比如说,类型 kristik 和类型主题。 When I'm implementing type kristik, it got successful response, but when it comes to type motif, the response is error.
当我实现类型 kristik 时,它得到了成功的响应,但是当涉及到类型主题时,响应是错误的。
here my code that has the error in console这里我的代码在控制台中有错误
componentWillMount() => {
const { dispatch } = this.props;
dispatch(getAllMotif());
}
I got error in dispatch(getAllMotif());
我在
dispatch(getAllMotif());
in commponentWillMount()
在
commponentWillMount()
Here my getAllMotif()
code这是我的
getAllMotif()
代码
getAllMotif(token) {
const path = `motif`;
const method = 'get';
return request.process(method, path, null, token);
},
Here my sagas getAllMotif
code这是我的
sagas getAllMotif
代码
export function* getAllMotif() {
try {
let { detail } = yield select(state => state.user);
const result = yield call(API.getAllMotif, detail.api_token);
yield put({
type: types.GET_ALL_MOTIF_SUCCESS,
payload: result,
});
} catch (err) {
yield put(handleError(err));
yield put({
type: types.GET_ALL_MOTIF_FAILURE,
payload: err,
});
}
}
here my reducer这是我的减速机
case types.GET_ALL_MOTIF_SUCCESS:
return {
...state,
motif: [
...action.payload.data.data
]
};
here my request code这是我的请求代码
internals.process = (method, path, payload, token, contentType=internals.contentType) => {
const request = {
url: `${API_URL}/${path}`,
method: method,
headers: {
'Content-Type': contentType,
'Accept': 'application/json',
},
};
if (token) {
request.params = {
token: token,
};
}
if (payload) {
request.data = payload;
}
return axios.request(request)
.then(res => {
if (![200, 201].includes(res.status)) {
throw new Error(res.status);
}
return res.data;
})
.catch((error) => {
console.error(method, path, error);
return Promise.reject({
message: error.response.data.error,
code: error.response.status
});
});
};
I don't know why in this type get error, because in type kristik also have very similar code.我不知道为什么在这种类型中会出错,因为在类型 kristik 中也有非常相似的代码。
You didn't dispatch an action that wasn't a plain object, your function getAllMotif
not return a plain object.您没有发送不是普通 object 的操作,您的 function
getAllMotif
没有返回普通 object。 That lead to the error here.这导致了这里的错误。
You should dispatch an normal action你应该派发一个正常的动作
getAllMotifAction(token) {
const path = `motif`;
const method = 'get';
return { type: 'GET_ALL_MOTIF', data: { path, method } };
},
Then in in saga, you catch this action and handle it with your saga function然后在 saga 中,你抓住这个动作并用你的 saga function 处理它
takeLatest('GET_ALL_MOTIF', getAllMotif);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.