Javascript Redux操作创建者在另一个内部调用一个
我正在使用Redux,但这应该与问题无关。我在同一个文件中有两个action Creator,我希望从另一个文件中调用其中一个(希望不必在下面的切换中对每种情况进行双重分派): 如果可能,我只想调用close一次,然后返回指定的openJavascript Redux操作创建者在另一个内部调用一个,javascript,redux,react-redux,Javascript,Redux,React Redux,我正在使用Redux,但这应该与问题无关。我在同一个文件中有两个action Creator,我希望从另一个文件中调用其中一个(希望不必在下面的切换中对每种情况进行双重分派): 如果可能,我只想调用close一次,然后返回指定的open 蒂亚 您需要使用类似的中间件。安装说明,但基本上您需要在设置店铺时应用它: const store = createStore( rootReducer, applyMiddleware(thunk) ); 这将允许您从操作返回函数。像这样的东西应该可
蒂亚 您需要使用类似的中间件。安装说明,但基本上您需要在设置店铺时应用它:
const store = createStore(
rootReducer,
applyMiddleware(thunk)
);
这将允许您从操作返回函数。像这样的东西应该可以做到:
export function openDialog(dialog) {
return (dispatch) => {
dispatch(closeAuthDialogs());
switch (dialog) {
case 'login':
return dispatch({type: OPEN_LOGIN})
case 'register':
return dispatch({type: OPEN_REGISTER})
case 'forgot':
return dispatch({type: OPEN_FORGOT})
case 'change':
return dispatch({type: OPEN_CHANGE})
case 'profile':
return dispatch({type: OPEN_PROFILE})
default:
return;
}
}
}
谢谢我有想法。只是在close dispatch中丢失了返回值,并将它们全部包装在其中。是的,如果使用thunk,则内部函数的返回值没有任何影响,您必须显式地
dispatch
,因为它是一个参数。
const store = createStore(
rootReducer,
applyMiddleware(thunk)
);
export function openDialog(dialog) {
return (dispatch) => {
dispatch(closeAuthDialogs());
switch (dialog) {
case 'login':
return dispatch({type: OPEN_LOGIN})
case 'register':
return dispatch({type: OPEN_REGISTER})
case 'forgot':
return dispatch({type: OPEN_FORGOT})
case 'change':
return dispatch({type: OPEN_CHANGE})
case 'profile':
return dispatch({type: OPEN_PROFILE})
default:
return;
}
}
}