Access store from routes file without using `connect`
See original GitHub issueI would like to make use of react-router’s onEnter
handler in order to prompt users to authenticate when entering a restricted route.
So far my routes.js
file looks something like this:
import React from 'react';
import { Route, IndexRoute } from 'react-router';
export default (
<Route path="/" component={App}>
<IndexRoute component={Landing} />
<Route path="learn" component={Learn} />
<Route path="about" component={About} />
<Route path="downloads" component={Downloads} onEnter={requireAuth} />
</Route>
)
Ideally, I’d like my requireAuth
function to be a redux action that has access to the store and current state, that works like this: store.dispatch(requireAuth())
.
Unfortunately I don’t have access to the store in this file. I don’t think I can use really use connect
in this case to access the relevant actions that I want, because the thing I’m exporting is not a component. I also can’t just import store
from the file where the store is created, as this is undefined when the app first loads.
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Access State in Store From Routing Setup File [react-router v4 ...
First you need to connect your component to the Redux Store. First of all import the connect method: import connect from "react-redux".
Read more >How to access store in routes.js file (not index.js)?
I have a typical routes.js file like this and i cannot access the store.even i am able to dispatch an action for the...
Read more >89. Accessing the store state data in the router ... - YouTube
Accessing the store state data in the router Navigation Guards in Vue router - Vue js 3 | Vue 3. Watch later. Share....
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 >Accessing Vuex store in router file - Laracasts
I am trying to access vuex store in the router file, but it's not working. ... router file: import VueRouter from "vue-router"; import...
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
Thank you @gaearon!! It’s nice to be able to still have
routes
in a separated file.BTW, I really like your Redux training on egghead.io. I really enjoy the way you describe the idea, then do a simple implementation, and then finally add layers of abstractions at the end. It’s the best training video I’ve seen in the last several years. Thank you!
@jungejason Thank you for kind words!