I want to redirect to a new page after a successful login. Routes (V4) are used as follows:
import { browserHistory } from '....browser_history_signleton';
...
class App extends Component {
render() {
const { authentication: { isSignedIn } } = this.props;
return (
<ConnectedRouter history={browserHistory}>
<div>
<Header/>
<Route exact path="/" component={Home}/>
<PrivateRoute isAuthorized={isSignedIn} path="/page1" component={PageOne}/>
<PrivateRoute isAuthorized={isSignedIn} path="/page2" component={PageTwo}/>
</div>
</ConnectedRouter>
);
}
}
The saga looks like this:
import { browserHistory } from '....browser_history_signleton';
export function* loginSaga() {
while (true) {
try {
const payload = yield take(LOGIN_SUBMIT);
const raceResult = yield race({
signin: call(loginRequest, payload),
logout: take('LOGOUT')
});
if (raceResult.signin) {
const { data } = raceResult.signin;
yield put(loginRequestSucceeded(data));
const redirectUrl = `.....based on location.state.from.pathname`
browserHistory.push(rediretUrl);
...
My main problem is how to share browserHistory.
createHistoryof the historymodule is not a subscriber, so I had to add:
import createHistory from 'history/createBrowserHistory';
export const browserHistory = createHistory();
What is the most efficient way to provide an instance historyfor a saga?
source
share