I have a user profile page where the user can update the data. Once the data is submitted, it should be redirected to dashboard page.
MyProfile.js
handleProfileUpdate = () => {
let user = this.state;
this.props.updateUserProfile(user);
}
Action is defined as follows
export const updateUserProfile = (obj) => ({
type: types.USER_PROFILE_UPDATE_REQUEST,
payload: obj
});
Saga file is defined as follows.
function* updateUserProfile(action) {
try {
let data = action.payload;
yield call(userProvider.updateUserProfile, data);
// dispatch a success action to the store
yield put({ type: types.USER_PROFILE_UPDATE_SUCCESS, data });
yield put(push('/dashboard'));
yield put(constants.successMsg(userProfileUpdateSuccess));
} catch (error) {
// dispatch a failure action to the store with the error
yield put(constants.DEFAULT_ERROR_MSG);
}
}
export function* watchUserProfileUpdateRequest() {
yield takeLatest(types.USER_PROFILE_UPDATE_REQUEST, updateUserProfile);
}
export default function* userSaga() {
yield all([
watchUserProfileRequest(),
watchUserProfileUpdateRequest()
]);
}
But the code yield put(push('/dashboard')) is not redirecting on to the page.
Any idea on how to fix this?
I founded a solution myself
history.js
import createHistory from 'history/createBrowserHistory';
const history = createHistory();
export default history;
Import history file into the saga
import history from '../../history';
Modify the watcher saga as follows.
function* updateUserProfile(action) {
try {
let data = action.payload;
yield call(userProvider.updateUserProfile, data);
// dispatch a success action to the store
yield put({ type: types.USER_PROFILE_UPDATE_SUCCESS, data });
yield call(forwardTo, '/dashboard');
yield put(constants.successMsg(userProfileUpdateSuccess));
} catch (error) {
// dispatch a failure action to the store with the error
yield put(constants.DEFAULT_ERROR_MSG);
}
}
Now, you need to define the forwardTo function as follows.
function forwardTo(location) {
history.push(location);
}