Trying to get the store before route creation
See original GitHub issueHi!
I’m trying to get the proper store in the create_routes function. I need to instanciate services before creating the routes in order to be able to use them in the Route callbacks (onEnter). Basically, my routes callbacks need the store and additionnals services. I tried to add some logics in the react-isomorphic-render file to be able to inject my services to the create_routes function and to the wrapper. Something like that:
import { IntlProvider, addLocaleData } from 'react-intl';
import frLocaleData from 'react-intl/locale-data/fr';
addLocaleData(frLocaleData);
import translations from './helpers/Translations';
import { ListUrl, Seo, Api, References, Security } from 'services';
import routes from './routes'
import Wrapper from './wrapper'
const locale = 'en';
const host = 'en';
const client = new Api();
const messages = translations(locale);
const rel = new IntlProvider({locale, messages, children: []}, {});
const intl = rel.getChildContext().intl;
const references = new References();
let services;
export default
{
reducer: () => require('store/modules/reducer'),
routes: (store) => {
const listUrl = new ListUrl(store, intl, references);
const seo = new Seo(intl, locale, host, listUrl);
services = {listUrl, seo, intl, api: client, references};
return routes(store, services.listUrl, api, intl);
},
wrapper: (props) => {
const newProps = {
...props,
services,
locale,
messages
};
return (<Wrapper {...newProps} />);
},
on_store_created({ reload_reducer }) {
if (_development_ && module.hot) {
module.hot.accept('./store/modules/reducer', reload_reducer)
}
}
}
and my routes file looks like that:
export default (store, listUrl, api, intl) => {
const requireLogin = hookAuth(store);
const requireLogout = hookNoAuth(store);
const checkList = hookList(store, listUrl, api);
const checkItem = hookItem(store, listUrl);
const getComponentList = componentList(store, listUrl, api);
const requireLoginAction = hookAuth(store, "actions");
/**
* Please keep routes in alphabetical order
*/
return (
<Route path="/" component={App}>
<IndexRoute component={Home} />
<Route component={Layout}>
<Route path="login" onEnter={requireLogout} component={Login} />
I don’t know how to achieve this. I’d really like to move from react-isomorphic-tools to universal-webpack and your complete universal stack. Help would be really appreciate. Thanks !
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Accessing Vuex state when defining Vue-Router routes
I'm trying to make it so that, if Vuex stores the user object, and it has the authenticated property set to false ,...
Read more >Can not use store in router.beforeEach #723 - GitHub
The function will run on import and since this happens before the Pinia instance has been created it won't work. The second example...
Read more >React Router with Redux: Understanding navigation state
With Redux, the state of your application is kept in a store, and each component can access any state that it needs from...
Read more >How To Handle Routing in React Apps with React Router
In this case, you'll want the following routes: /manataee , /narwhal , and /whale . The Route component will take a path as...
Read more >Express Tutorial Part 4: Routes and controllers - MDN Web Docs
Before we define our routes, we'll first create all the dummy/skeleton callback functions that they will invoke. The callbacks will be stored in ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok, version
4.1.7
should do itThanks !! Solved my problem !! Great job 😃