Prevent react-router from load routes before redux is loaded
up vote
1
down vote
favorite
I have a pretty simple setup with my app looking like:
<Provider store=store>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>
And a router switch like
<Switch>
<Route path='/test' component=comp1/>
<Route exact path='/' component=comp2/>
</Switch>
I haven't worked with routes much but I want to prevent those two routes from loading before my redux fetch is finished but I can't seem to get it working. I've tried checking if the data is loaded in redux, basically wrapping the switch with a check of
this.props.data.size > 0
but it seems to render anyway. They both use the same data so it seems like bad practice to fetch data into redux individually in both components when I should be able to just fetch once regardless of which route was loaded.
reactjs redux react-router
add a comment |
up vote
1
down vote
favorite
I have a pretty simple setup with my app looking like:
<Provider store=store>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>
And a router switch like
<Switch>
<Route path='/test' component=comp1/>
<Route exact path='/' component=comp2/>
</Switch>
I haven't worked with routes much but I want to prevent those two routes from loading before my redux fetch is finished but I can't seem to get it working. I've tried checking if the data is loaded in redux, basically wrapping the switch with a check of
this.props.data.size > 0
but it seems to render anyway. They both use the same data so it seems like bad practice to fetch data into redux individually in both components when I should be able to just fetch once regardless of which route was loaded.
reactjs redux react-router
Can you clarify whywrapping the switch with a check
doesn't work?
– Alex
Nov 10 at 4:01
Andddd I'm an idiot and forgot I changed the return data from an object to an array so size would never work. Changing the check to length fixed it.
– ben54321
Nov 10 at 4:13
Ye,. The only thing I'd do is - to wrap it into additional component, and wrapswitch
into the wrapper. But general idea direction seems to be ok.
– Alex
Nov 10 at 4:15
So you're thinking wrap the routes with a wrapper component containing the switch?
– ben54321
Nov 10 at 18:39
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a pretty simple setup with my app looking like:
<Provider store=store>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>
And a router switch like
<Switch>
<Route path='/test' component=comp1/>
<Route exact path='/' component=comp2/>
</Switch>
I haven't worked with routes much but I want to prevent those two routes from loading before my redux fetch is finished but I can't seem to get it working. I've tried checking if the data is loaded in redux, basically wrapping the switch with a check of
this.props.data.size > 0
but it seems to render anyway. They both use the same data so it seems like bad practice to fetch data into redux individually in both components when I should be able to just fetch once regardless of which route was loaded.
reactjs redux react-router
I have a pretty simple setup with my app looking like:
<Provider store=store>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>
And a router switch like
<Switch>
<Route path='/test' component=comp1/>
<Route exact path='/' component=comp2/>
</Switch>
I haven't worked with routes much but I want to prevent those two routes from loading before my redux fetch is finished but I can't seem to get it working. I've tried checking if the data is loaded in redux, basically wrapping the switch with a check of
this.props.data.size > 0
but it seems to render anyway. They both use the same data so it seems like bad practice to fetch data into redux individually in both components when I should be able to just fetch once regardless of which route was loaded.
reactjs redux react-router
reactjs redux react-router
asked Nov 10 at 3:56
ben54321
305
305
Can you clarify whywrapping the switch with a check
doesn't work?
– Alex
Nov 10 at 4:01
Andddd I'm an idiot and forgot I changed the return data from an object to an array so size would never work. Changing the check to length fixed it.
– ben54321
Nov 10 at 4:13
Ye,. The only thing I'd do is - to wrap it into additional component, and wrapswitch
into the wrapper. But general idea direction seems to be ok.
– Alex
Nov 10 at 4:15
So you're thinking wrap the routes with a wrapper component containing the switch?
– ben54321
Nov 10 at 18:39
add a comment |
Can you clarify whywrapping the switch with a check
doesn't work?
– Alex
Nov 10 at 4:01
Andddd I'm an idiot and forgot I changed the return data from an object to an array so size would never work. Changing the check to length fixed it.
– ben54321
Nov 10 at 4:13
Ye,. The only thing I'd do is - to wrap it into additional component, and wrapswitch
into the wrapper. But general idea direction seems to be ok.
– Alex
Nov 10 at 4:15
So you're thinking wrap the routes with a wrapper component containing the switch?
– ben54321
Nov 10 at 18:39
Can you clarify why
wrapping the switch with a check
doesn't work?– Alex
Nov 10 at 4:01
Can you clarify why
wrapping the switch with a check
doesn't work?– Alex
Nov 10 at 4:01
Andddd I'm an idiot and forgot I changed the return data from an object to an array so size would never work. Changing the check to length fixed it.
– ben54321
Nov 10 at 4:13
Andddd I'm an idiot and forgot I changed the return data from an object to an array so size would never work. Changing the check to length fixed it.
– ben54321
Nov 10 at 4:13
Ye,. The only thing I'd do is - to wrap it into additional component, and wrap
switch
into the wrapper. But general idea direction seems to be ok.– Alex
Nov 10 at 4:15
Ye,. The only thing I'd do is - to wrap it into additional component, and wrap
switch
into the wrapper. But general idea direction seems to be ok.– Alex
Nov 10 at 4:15
So you're thinking wrap the routes with a wrapper component containing the switch?
– ben54321
Nov 10 at 18:39
So you're thinking wrap the routes with a wrapper component containing the switch?
– ben54321
Nov 10 at 18:39
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Your general idea checking if the data is loaded in redux
seems to be ok, the only thing I'd suggest you is to encapsulate this logic into a wrapper component:
export const CheckLoadedApp = (props) => this.props.data.lenght ? <App /> : 'Loading...';
And change your codebase accordingly:
<Provider store=store>
<BrowserRouter>
<CheckLoadedApp />
</BrowserRouter>
</Provider>
But, up to you :)
Ah gotcha. I do like this idea in order to de couple that logic from the router.
– ben54321
Nov 11 at 3:33
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Your general idea checking if the data is loaded in redux
seems to be ok, the only thing I'd suggest you is to encapsulate this logic into a wrapper component:
export const CheckLoadedApp = (props) => this.props.data.lenght ? <App /> : 'Loading...';
And change your codebase accordingly:
<Provider store=store>
<BrowserRouter>
<CheckLoadedApp />
</BrowserRouter>
</Provider>
But, up to you :)
Ah gotcha. I do like this idea in order to de couple that logic from the router.
– ben54321
Nov 11 at 3:33
add a comment |
up vote
0
down vote
Your general idea checking if the data is loaded in redux
seems to be ok, the only thing I'd suggest you is to encapsulate this logic into a wrapper component:
export const CheckLoadedApp = (props) => this.props.data.lenght ? <App /> : 'Loading...';
And change your codebase accordingly:
<Provider store=store>
<BrowserRouter>
<CheckLoadedApp />
</BrowserRouter>
</Provider>
But, up to you :)
Ah gotcha. I do like this idea in order to de couple that logic from the router.
– ben54321
Nov 11 at 3:33
add a comment |
up vote
0
down vote
up vote
0
down vote
Your general idea checking if the data is loaded in redux
seems to be ok, the only thing I'd suggest you is to encapsulate this logic into a wrapper component:
export const CheckLoadedApp = (props) => this.props.data.lenght ? <App /> : 'Loading...';
And change your codebase accordingly:
<Provider store=store>
<BrowserRouter>
<CheckLoadedApp />
</BrowserRouter>
</Provider>
But, up to you :)
Your general idea checking if the data is loaded in redux
seems to be ok, the only thing I'd suggest you is to encapsulate this logic into a wrapper component:
export const CheckLoadedApp = (props) => this.props.data.lenght ? <App /> : 'Loading...';
And change your codebase accordingly:
<Provider store=store>
<BrowserRouter>
<CheckLoadedApp />
</BrowserRouter>
</Provider>
But, up to you :)
answered Nov 10 at 19:24
Alex
2,990620
2,990620
Ah gotcha. I do like this idea in order to de couple that logic from the router.
– ben54321
Nov 11 at 3:33
add a comment |
Ah gotcha. I do like this idea in order to de couple that logic from the router.
– ben54321
Nov 11 at 3:33
Ah gotcha. I do like this idea in order to de couple that logic from the router.
– ben54321
Nov 11 at 3:33
Ah gotcha. I do like this idea in order to de couple that logic from the router.
– ben54321
Nov 11 at 3:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235875%2fprevent-react-router-from-load-routes-before-redux-is-loaded%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you clarify why
wrapping the switch with a check
doesn't work?– Alex
Nov 10 at 4:01
Andddd I'm an idiot and forgot I changed the return data from an object to an array so size would never work. Changing the check to length fixed it.
– ben54321
Nov 10 at 4:13
Ye,. The only thing I'd do is - to wrap it into additional component, and wrap
switch
into the wrapper. But general idea direction seems to be ok.– Alex
Nov 10 at 4:15
So you're thinking wrap the routes with a wrapper component containing the switch?
– ben54321
Nov 10 at 18:39