【发布时间】:2021-07-31 10:16:30
【问题描述】:
我正在使用 redux 构建一个反应应用程序。当用户提交表单时,有两种可能的结果:错误或成功。
这是动作创建者:
export const createSomething = (data) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
firestore.collection('data_s').add( {
...data,
}).then((dts) =>{
dispatch({
type : 'CREATED_SUCCESS', dts
});
}).catch((err) => {
dispatch({
type : 'CREATE_ERROR', err
});
})
}
};
和减速器:
const createSomethingReducer = (state = initState, action) => {
switch (action.type) {
case 'CREATED_SUCCESS' :
console.log('created', action.data);
return state;
case 'CREATE_ERROR' :
console.log('create error', action.err);
return state;
default :
return state
}
}
提交是这样完成的
handleSubmit = (e) => {
e.preventDefault();
this.props.createEmployee(this.state)
}
如果表单提交成功,我将如何定向到“/success”,如果表单没有通过,我将如何定向到“/error”?
我尝试过使用
this.props.history.push('/app/dashboard')
但是无论结果如何,这都会重定向。请帮忙。
谢谢
【问题讨论】:
标签: reactjs redux react-router-dom