How to render a component only for one Route?












0














I am trying to remove Header component from route page only . How can i do this in React router 4 ?



this is my Routes



const AppRouter: any = () => (
<BrowserRouter>
<React.Fragment>
<Header />
<Switch>
<Route path={LOOP_MAIN_ROUTE} component={LandingPage} exact/>
<Route path={LOOP_LOGIN_ROUTE} component={LoginPage} exact/>
<ProtectedRoute path={LOOP_SEARCH_ROUTE} component={SearchPage} />
<ProtectedRoute path={LOOP_OFFER_ROUTE} component={Offer} exact />
<ProtectedRoute path={LOOP_OFFER_APPROVAL} component={OfferApproval} exact />
<ProtectedRoute path={LOOP_OFFER_CONFIRMATION} component={OfferConfirmation} exact />
</Switch>
</React.Fragment>
</BrowserRouter>
);

export default AppRouter;









share|improve this question






















  • for what path Header should render?
    – Sagiv b.g
    Nov 12 '18 at 15:15










  • there is no path ... First i have tried to render it for all routes , but now i need to NOT render for LOOP_MAIN_ROUTE
    – Norayr Ghukasyan
    Nov 12 '18 at 15:17










  • you can pass a regex, see this answer it may help
    – Sagiv b.g
    Nov 12 '18 at 15:20










  • @NorayrGhukasyan Consider updating the question with this information because the question says the opposite.
    – estus
    Nov 12 '18 at 15:32
















0














I am trying to remove Header component from route page only . How can i do this in React router 4 ?



this is my Routes



const AppRouter: any = () => (
<BrowserRouter>
<React.Fragment>
<Header />
<Switch>
<Route path={LOOP_MAIN_ROUTE} component={LandingPage} exact/>
<Route path={LOOP_LOGIN_ROUTE} component={LoginPage} exact/>
<ProtectedRoute path={LOOP_SEARCH_ROUTE} component={SearchPage} />
<ProtectedRoute path={LOOP_OFFER_ROUTE} component={Offer} exact />
<ProtectedRoute path={LOOP_OFFER_APPROVAL} component={OfferApproval} exact />
<ProtectedRoute path={LOOP_OFFER_CONFIRMATION} component={OfferConfirmation} exact />
</Switch>
</React.Fragment>
</BrowserRouter>
);

export default AppRouter;









share|improve this question






















  • for what path Header should render?
    – Sagiv b.g
    Nov 12 '18 at 15:15










  • there is no path ... First i have tried to render it for all routes , but now i need to NOT render for LOOP_MAIN_ROUTE
    – Norayr Ghukasyan
    Nov 12 '18 at 15:17










  • you can pass a regex, see this answer it may help
    – Sagiv b.g
    Nov 12 '18 at 15:20










  • @NorayrGhukasyan Consider updating the question with this information because the question says the opposite.
    – estus
    Nov 12 '18 at 15:32














0












0








0







I am trying to remove Header component from route page only . How can i do this in React router 4 ?



this is my Routes



const AppRouter: any = () => (
<BrowserRouter>
<React.Fragment>
<Header />
<Switch>
<Route path={LOOP_MAIN_ROUTE} component={LandingPage} exact/>
<Route path={LOOP_LOGIN_ROUTE} component={LoginPage} exact/>
<ProtectedRoute path={LOOP_SEARCH_ROUTE} component={SearchPage} />
<ProtectedRoute path={LOOP_OFFER_ROUTE} component={Offer} exact />
<ProtectedRoute path={LOOP_OFFER_APPROVAL} component={OfferApproval} exact />
<ProtectedRoute path={LOOP_OFFER_CONFIRMATION} component={OfferConfirmation} exact />
</Switch>
</React.Fragment>
</BrowserRouter>
);

export default AppRouter;









share|improve this question













I am trying to remove Header component from route page only . How can i do this in React router 4 ?



this is my Routes



const AppRouter: any = () => (
<BrowserRouter>
<React.Fragment>
<Header />
<Switch>
<Route path={LOOP_MAIN_ROUTE} component={LandingPage} exact/>
<Route path={LOOP_LOGIN_ROUTE} component={LoginPage} exact/>
<ProtectedRoute path={LOOP_SEARCH_ROUTE} component={SearchPage} />
<ProtectedRoute path={LOOP_OFFER_ROUTE} component={Offer} exact />
<ProtectedRoute path={LOOP_OFFER_APPROVAL} component={OfferApproval} exact />
<ProtectedRoute path={LOOP_OFFER_CONFIRMATION} component={OfferConfirmation} exact />
</Switch>
</React.Fragment>
</BrowserRouter>
);

export default AppRouter;






reactjs routing react-router






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 15:10









Norayr Ghukasyan

1099




1099












  • for what path Header should render?
    – Sagiv b.g
    Nov 12 '18 at 15:15










  • there is no path ... First i have tried to render it for all routes , but now i need to NOT render for LOOP_MAIN_ROUTE
    – Norayr Ghukasyan
    Nov 12 '18 at 15:17










  • you can pass a regex, see this answer it may help
    – Sagiv b.g
    Nov 12 '18 at 15:20










  • @NorayrGhukasyan Consider updating the question with this information because the question says the opposite.
    – estus
    Nov 12 '18 at 15:32


















  • for what path Header should render?
    – Sagiv b.g
    Nov 12 '18 at 15:15










  • there is no path ... First i have tried to render it for all routes , but now i need to NOT render for LOOP_MAIN_ROUTE
    – Norayr Ghukasyan
    Nov 12 '18 at 15:17










  • you can pass a regex, see this answer it may help
    – Sagiv b.g
    Nov 12 '18 at 15:20










  • @NorayrGhukasyan Consider updating the question with this information because the question says the opposite.
    – estus
    Nov 12 '18 at 15:32
















for what path Header should render?
– Sagiv b.g
Nov 12 '18 at 15:15




for what path Header should render?
– Sagiv b.g
Nov 12 '18 at 15:15












there is no path ... First i have tried to render it for all routes , but now i need to NOT render for LOOP_MAIN_ROUTE
– Norayr Ghukasyan
Nov 12 '18 at 15:17




there is no path ... First i have tried to render it for all routes , but now i need to NOT render for LOOP_MAIN_ROUTE
– Norayr Ghukasyan
Nov 12 '18 at 15:17












you can pass a regex, see this answer it may help
– Sagiv b.g
Nov 12 '18 at 15:20




you can pass a regex, see this answer it may help
– Sagiv b.g
Nov 12 '18 at 15:20












@NorayrGhukasyan Consider updating the question with this information because the question says the opposite.
– estus
Nov 12 '18 at 15:32




@NorayrGhukasyan Consider updating the question with this information because the question says the opposite.
– estus
Nov 12 '18 at 15:32












2 Answers
2






active

oldest

votes


















1














One way to do this is using matchPath. For instance in your Header component:



import React from "react";
import {LOOP_MAIN_ROUTE} from "whereverYouHaveThisConstant";
import { withRouter, matchPath } from 'react-router-dom';
const Header = (props) => {
const suppressHeader = matchPath(props.location.pathname, {path: LOOP_MAIN_ROUTE});
if (suppressHeader ) {
return null;
}
return <>your header stuff</>;
};
export default withRouter(Header);





share|improve this answer





















  • This works fine , only i change the suppressHeader whit suppressHeader.isExact
    – Norayr Ghukasyan
    Nov 12 '18 at 15:38



















2














Header can be conditionally rendered as a route:



<Route render={
({ location: { pathname } }) => pathname !== LOOP_MAIN_ROUTE && <Header/>
}/>





share|improve this answer





















  • I think this is more elegant solution
    – Norayr Ghukasyan
    Nov 13 '18 at 6:22











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',
autoActivateHeartbeat: false,
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%2f53264995%2fhow-to-render-a-component-only-for-one-route%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














One way to do this is using matchPath. For instance in your Header component:



import React from "react";
import {LOOP_MAIN_ROUTE} from "whereverYouHaveThisConstant";
import { withRouter, matchPath } from 'react-router-dom';
const Header = (props) => {
const suppressHeader = matchPath(props.location.pathname, {path: LOOP_MAIN_ROUTE});
if (suppressHeader ) {
return null;
}
return <>your header stuff</>;
};
export default withRouter(Header);





share|improve this answer





















  • This works fine , only i change the suppressHeader whit suppressHeader.isExact
    – Norayr Ghukasyan
    Nov 12 '18 at 15:38
















1














One way to do this is using matchPath. For instance in your Header component:



import React from "react";
import {LOOP_MAIN_ROUTE} from "whereverYouHaveThisConstant";
import { withRouter, matchPath } from 'react-router-dom';
const Header = (props) => {
const suppressHeader = matchPath(props.location.pathname, {path: LOOP_MAIN_ROUTE});
if (suppressHeader ) {
return null;
}
return <>your header stuff</>;
};
export default withRouter(Header);





share|improve this answer





















  • This works fine , only i change the suppressHeader whit suppressHeader.isExact
    – Norayr Ghukasyan
    Nov 12 '18 at 15:38














1












1








1






One way to do this is using matchPath. For instance in your Header component:



import React from "react";
import {LOOP_MAIN_ROUTE} from "whereverYouHaveThisConstant";
import { withRouter, matchPath } from 'react-router-dom';
const Header = (props) => {
const suppressHeader = matchPath(props.location.pathname, {path: LOOP_MAIN_ROUTE});
if (suppressHeader ) {
return null;
}
return <>your header stuff</>;
};
export default withRouter(Header);





share|improve this answer












One way to do this is using matchPath. For instance in your Header component:



import React from "react";
import {LOOP_MAIN_ROUTE} from "whereverYouHaveThisConstant";
import { withRouter, matchPath } from 'react-router-dom';
const Header = (props) => {
const suppressHeader = matchPath(props.location.pathname, {path: LOOP_MAIN_ROUTE});
if (suppressHeader ) {
return null;
}
return <>your header stuff</>;
};
export default withRouter(Header);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 15:25









Ryan Cogswell

1,516211




1,516211












  • This works fine , only i change the suppressHeader whit suppressHeader.isExact
    – Norayr Ghukasyan
    Nov 12 '18 at 15:38


















  • This works fine , only i change the suppressHeader whit suppressHeader.isExact
    – Norayr Ghukasyan
    Nov 12 '18 at 15:38
















This works fine , only i change the suppressHeader whit suppressHeader.isExact
– Norayr Ghukasyan
Nov 12 '18 at 15:38




This works fine , only i change the suppressHeader whit suppressHeader.isExact
– Norayr Ghukasyan
Nov 12 '18 at 15:38













2














Header can be conditionally rendered as a route:



<Route render={
({ location: { pathname } }) => pathname !== LOOP_MAIN_ROUTE && <Header/>
}/>





share|improve this answer





















  • I think this is more elegant solution
    – Norayr Ghukasyan
    Nov 13 '18 at 6:22
















2














Header can be conditionally rendered as a route:



<Route render={
({ location: { pathname } }) => pathname !== LOOP_MAIN_ROUTE && <Header/>
}/>





share|improve this answer





















  • I think this is more elegant solution
    – Norayr Ghukasyan
    Nov 13 '18 at 6:22














2












2








2






Header can be conditionally rendered as a route:



<Route render={
({ location: { pathname } }) => pathname !== LOOP_MAIN_ROUTE && <Header/>
}/>





share|improve this answer












Header can be conditionally rendered as a route:



<Route render={
({ location: { pathname } }) => pathname !== LOOP_MAIN_ROUTE && <Header/>
}/>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 15:33









estus

67.2k2199214




67.2k2199214












  • I think this is more elegant solution
    – Norayr Ghukasyan
    Nov 13 '18 at 6:22


















  • I think this is more elegant solution
    – Norayr Ghukasyan
    Nov 13 '18 at 6:22
















I think this is more elegant solution
– Norayr Ghukasyan
Nov 13 '18 at 6:22




I think this is more elegant solution
– Norayr Ghukasyan
Nov 13 '18 at 6:22


















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%2f53264995%2fhow-to-render-a-component-only-for-one-route%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

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues