Router / History

@react-sate/store uses two kind of router history

  • createMemoryHistory

  • createBrowserHistory

createMemoryHistory - is used when SSR is enabled and when running unit tests wth ReactStateTestBed

createBrowserHistory - is used in browser app

In order to do redirects you need to do history.push and pass route you want to navigate to. Usually, in React, history is passed via props. However there are cases where you would like to do navigation from place where props are not accessible - like from service e.g. you would like to redirect user to login page from auth service or from http service if response is 401.

For this reason @react-state/store exposes history object. It can be accessed via RouterHistory class.

import { RouterHistory } from '@react-state/store';
...
RouterHistory.history.push('/login');

Last updated