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.










share|improve this question





















  • 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














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.










share|improve this question





















  • 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












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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 3:56









ben54321

305




305











  • 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
















  • 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















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












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 :)






share|improve this answer




















  • Ah gotcha. I do like this idea in order to de couple that logic from the router.
    – ben54321
    Nov 11 at 3:33










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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 :)






share|improve this answer




















  • Ah gotcha. I do like this idea in order to de couple that logic from the router.
    – ben54321
    Nov 11 at 3:33














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 :)






share|improve this answer




















  • Ah gotcha. I do like this idea in order to de couple that logic from the router.
    – ben54321
    Nov 11 at 3:33












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 :)






share|improve this answer












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 :)







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo