Answer the question
In order to leave comments, you need to log in
How do you render components depending on the route?
Asked a similar question on StackOverflow , got some interesting answers. But interested in the practical experience of developers.
In short - periodically there is a task to render some component (header, footer, sidebar, whatever) only on some routes. Let's say there are 100 routes in the application, 50 of them should have a header, 50 should not.
As suggestions, I received options:
1) Use HOC
2) Here is the following construction:
...
<Switch>
<Route path="/noheader1" ... />
<Route path="/noheader2" ... />
<Route path="/noheader3" ... />
<Route component={HeaderRoutes} />
</Switch>
...
HeaderRoutes = props => (
<React.Fragment>
<Header/>
<Switch>
<Route path="/withheader1" ... />
<Route path="/withheader2" ... />
<Route path="/withheader3" ... />
</Switch>
</React.Fragment>
)
Answer the question
In order to leave comments, you need to log in
<Route path="/iwantrenderonthis" component={MyComponent} />
In terms of practical implementation, I would slightly overload the routes (in fact, they have to be overloaded for the visibility area)
Then you can wrap it in a Redux container and, according to some (only you know) condition, send a visibility parameter to it, depending on which you can render or do not render some component
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question