Simple custom hook that allows to "persist" values when navigating between routes.
Uses Redux for holding the state so all Redux tools and features are available.
Make sure that Redux is setup with the project.
Then, just add the reducer to the global composite reducer:
import { reducer as routeStateReducer } from 'global/hooks/useRouteState';
//...
export default combineReducers({
//...
routeState: routeStateReducer,
//...
});
import useRouteState from '.../hooks/useRouteState';
//...
function MyRouteComponent(props) {
const [someRouteValue, setSomeRouteValue] = useRouteState(
'MyRouteName',
'MyValueKey',
'Default value',
);
//...
}