2017-10-09 61 views
0

我有动作创建者的actions/index.js文件,而且我正在使用redux-thunk作为中间件。这是代码:Redux-thunk操作

export const fetchUser =() => async dispatch => { 
    const res = await axios.get('/api/current_user'); 
    dispatch({type: FETCH_USER, payload: res.data}); 
}; 

export const handleToken = (token) => async dispatch => { 
    const res = await axios.post('/api/stripe/', token); 

    dispatch({type: FETCH_USER, payload: res.data}); 
}; 

export const submitSurvey = (values, history) => async dispatch => { 
    const res = await axios.post('/api/Surveys', values); 

    history.push('/Surveys'); 
    dispatch({type: FETCH_USER, payload: res.data}); 
}; 

export const scrollMovement = (scrollValue) => dispatch => { 
    dispatch({type: SCROLL_MOVE, payload: scrollValue}) 
}; 

export const selectConfig = (id) => dispatch => { 
    dispatch({type: "SELECT_CONFIG", payload: id}) 
}; 

我有一个问题。我是否应该编写动作创建器,它不会像我编写hadleToken,submitSurvey和fetchUser动作创建器那样以相同样式向外部API发送请求(例如scrollMovement和selectConfig)?

我希望你能理解我的问题。如果不是,请添加评论,我会解释。

回答

0

对于数据的同步流,您只需要像往常一样返回一个对象,因为在生成调度之前,thunk中间件在类型函数中拦截了操作。

所以对于scrollMovementselectConfig你没有返回的功能,因为这些跟随的数据

export const scrollMovement = scrollValue => { 
    return {type: SCROLL_MOVE, payload: scrollValue} 
}; 

export const selectConfig = id => { 
    return {type: "SELECT_CONFIG", payload: id} 
}; 
+1

的同步流动'dispatch'是不确定的,但 –

+0

'在这种情况下dispatch'将是不确定的,是的 – Remzes

+1

对不起,你是对的我编辑我的帖子,你只需要返回一个对象,这是你的动作,如果你正在跟随一个同步流。 –