GibboK的回答已经指出了语法错误。
但是,我不认为你理解正确使用动作。你运行:
const request = axios.get(`${ROOT_URL}/posts${API_KEY}`);
这是创造一个承诺。您目前正在回复此操作。减速器意味着确定性的免费副作用&,因此为什么动作应该是纯JS对象。你不应该提交承诺。
相反,您应该使用一些相关的中间件,例如, redux-thunk,redux-saga或其他东西。当实际承诺已经解决时,您应该发送一个动作。
作为一个简单的人为的例子,使用redux-thunk
:
export const fetchPosts =() => (dispatch) => {
// Send action to notify request started. Reducers often want to
// update the state to record that a request is pending, e.g. for
// UI purposes.
dispatch({type: FETCH_POSTS_START});
// Start request
request = axios.get(`${ROOT_URL}/posts${API_KEY}`)
.then(res => {
// Successfully received posts, submit response data
dispatch({type: FETCH_POSTS_COMPLETE, payload: res.data})
})
.catch(err => {
// API call failed, submit error
dispatch({type: FETCH_POSTS_ERROR, payload: err})
});
};
注意此代码只是一个例子,并不一定适合于在生产系统中使用。
你应该返回一个对象。对象属性由','not';'定界,所以它应该是'type:FETCH_POSTS,' –
[Actions必须是React/Redux中的普通对象?](https://stackoverflow.com/questions/39693161 /动作-必须待纯对象 - 在反应的-终极版) –