Javascript &引用;无法读取属性';然后';“未定义”的定义;调度行动时
根据Redux文档提供的信息,我已将我的action creator设置为:Javascript &引用;无法读取属性';然后';“未定义”的定义;调度行动时,javascript,reactjs,redux,redux-thunk,Javascript,Reactjs,Redux,Redux Thunk,根据Redux文档提供的信息,我已将我的action creator设置为: export function createAlbumType(props) { return function (dispatch, getState) { dispatch({ type: CREATE_ALBUM_TYPE_REQUEST, }); axios.post(`${ALBUM_TYPES_URL}`, props).then( response =
export function createAlbumType(props) {
return function (dispatch, getState) {
dispatch({
type: CREATE_ALBUM_TYPE_REQUEST,
});
axios.post(`${ALBUM_TYPES_URL}`, props).then(
response => dispatch({
type: CREATE_ALBUM_TYPE_SUCCESS,
response,
}),
error => dispatch({
type: CREATE_ALBUM_TYPE_FAILURE,
error,
})
);
};
}
在我的相册类型成功保存后,我只想将用户重定向回索引页。不幸的是,派遣后我没有得到回复。根据我的代码,出现了什么问题?我使用redux-thunk
作为中间件,使用react-redux-router
进行路由
onCreateSubmit(values) {
const { dispatch } = this.props;
return dispatch(createAlbumType(values))
.then((action) => {
if (action.type === 'CREATE_ALBUM_TYPE_SUCCESS') {
dispatch(push('/album_types'));
}
});
}
const-store=createStore(reducer,{},compose(
applyMiddleware(
RouterMiddle软件(浏览器历史记录),
Thunk中间件
),
window.devToolsExtension?window.devToolsExtension():f=>f
)
);
const history=syncHistoryWithStore(浏览器历史记录,存储)
ReactDOM.render(
,
document.getElementById('root'))
);
redux操作没有返回承诺。您应该返回axios.post函数
export function createAlbumType(props) {
return function (dispatch, getState) {
dispatch({
type: CREATE_ALBUM_TYPE_REQUEST,
});
return axios.post(`${ALBUM_TYPES_URL}`, props).then(
response => dispatch({
type: CREATE_ALBUM_TYPE_SUCCESS,
response,
}),
error => dispatch({
type: CREATE_ALBUM_TYPE_FAILURE,
error,
})
);
};
}
redux操作没有返回承诺。您应该返回axios.post函数
export function createAlbumType(props) {
return function (dispatch, getState) {
dispatch({
type: CREATE_ALBUM_TYPE_REQUEST,
});
return axios.post(`${ALBUM_TYPES_URL}`, props).then(
response => dispatch({
type: CREATE_ALBUM_TYPE_SUCCESS,
response,
}),
error => dispatch({
type: CREATE_ALBUM_TYPE_FAILURE,
error,
})
);
};
}
我建议不要这样做:
return dispatch(createAlbumType(values))
.then((action) => {
if (action.type === 'CREATE_ALBUM_TYPE_SUCCESS') {
dispatch(push('/album_types'));
}
});
}
您在这里用这个
if(action.type)
重新发明了减速机。您应该在createAlbumType
操作中,在.post().then()中进行调度。这样,所有内容都包含在应该包含的位置。我建议不要这样做:
return dispatch(createAlbumType(values))
.then((action) => {
if (action.type === 'CREATE_ALBUM_TYPE_SUCCESS') {
dispatch(push('/album_types'));
}
});
}
您在这里用这个if(action.type)
重新发明了减速机。您应该在createAlbumType
操作中,在.post().then()中进行调度。通过这种方式,所有内容都包含在应该包含的位置。您是否建议将推送
方法放在action creator中?您是否对创建相册\u TYPE \u SUCCESS
操作做了其他事情?也许你可以直接调用dispatch(push('/album_types')代码>在中。然后(
。我想我会跟随你,但为了进一步澄清,你能提供一个完整的例子吗?你是否建议你将推送
方法放在动作创建者中?你对创建相册_类型(成功)
动作做了什么吗?也许你可以调用调度(推送)('/album_types');
在中。然后(
。我想我听你的,但为了进一步澄清,你能提供一个完整的例子吗?