我真的不知道为什么这个动作不起作用。在返回调度功能之前停止操作(仅记录'工作'
) export function authAdmin({nickname, password}){
console.log('working');
return function(dispatch){
console.log('still working');
axios.post(`${URL}/admin`, {nickname, password})
.then(response => {
console.log(response);
localStorage.setItem('token', response.data.token);
dispatch({type: AUTH_ADMIN});
browserHistory.push('/');
})
.catch((err) => {
console.log(err);
});
}
}
但几乎相同的行动没有问题。我错过了一些明显的东西吗?
export function getPosts(page){
return function(dispatch){
dispatch({ type: IS_FETCHING });
axios.get(`${URL}/home?page=${page}`)
.then(response => {
dispatch ({
type: FETCH_POSTS,
payload: response.data
})
})
.catch((error) => {
dispatch({ type: ERROR_FETCHING });
});
}
}
答案 0 :(得分:0)
后人:
这通常在你自己调用thunk函数时发生,这意味着内部函数没有传递给dispatch()
:
import {someThunkActionCreator} from "./actions";
// Wrong - just returns the inner function, but never runs it
someThunkActionCreator();
// Right - passes the inner function to dispatch, which runs it
dispatch(someThunkActionCreator())
// Right - creates a bound-up version which auto-dispatches
const boundThunk = bindActionCreators(someThunkActionCreator, dispatch);
boundThunk();