question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access store from routes file without using `connect`

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
jungejasoncommented, Apr 14, 2016

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!

3reactions
gaearoncommented, Apr 14, 2016

@jungejason Thank you for kind words!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found