How to render a component only for one Route?
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
add a comment |
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
for what pathHeader
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
add a comment |
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
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
reactjs routing react-router
asked Nov 12 '18 at 15:10
Norayr Ghukasyan
1099
1099
for what pathHeader
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
add a comment |
for what pathHeader
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
add a comment |
2 Answers
2
active
oldest
votes
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);
This works fine , only i change the suppressHeader whit suppressHeader.isExact
– Norayr Ghukasyan
Nov 12 '18 at 15:38
add a comment |
Header
can be conditionally rendered as a route:
<Route render={
({ location: { pathname } }) => pathname !== LOOP_MAIN_ROUTE && <Header/>
}/>
I think this is more elegant solution
– Norayr Ghukasyan
Nov 13 '18 at 6:22
add a comment |
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
});
}
});
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%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
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);
This works fine , only i change the suppressHeader whit suppressHeader.isExact
– Norayr Ghukasyan
Nov 12 '18 at 15:38
add a comment |
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);
This works fine , only i change the suppressHeader whit suppressHeader.isExact
– Norayr Ghukasyan
Nov 12 '18 at 15:38
add a comment |
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);
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);
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
add a comment |
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
add a comment |
Header
can be conditionally rendered as a route:
<Route render={
({ location: { pathname } }) => pathname !== LOOP_MAIN_ROUTE && <Header/>
}/>
I think this is more elegant solution
– Norayr Ghukasyan
Nov 13 '18 at 6:22
add a comment |
Header
can be conditionally rendered as a route:
<Route render={
({ location: { pathname } }) => pathname !== LOOP_MAIN_ROUTE && <Header/>
}/>
I think this is more elegant solution
– Norayr Ghukasyan
Nov 13 '18 at 6:22
add a comment |
Header
can be conditionally rendered as a route:
<Route render={
({ location: { pathname } }) => pathname !== LOOP_MAIN_ROUTE && <Header/>
}/>
Header
can be conditionally rendered as a route:
<Route render={
({ location: { pathname } }) => pathname !== LOOP_MAIN_ROUTE && <Header/>
}/>
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
add a comment |
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
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%2f53264995%2fhow-to-render-a-component-only-for-one-route%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
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