not getting location react router?
id like to change my header color when in my contact page. unable to get location from react router. I get "TypeError: Cannot read property 'pathname' of undefined".
I know I'm missing something simple but i cant seem to figure it out.
import React from 'react';
import './Header.css';
import NavLink from 'react-router-dom';
class Header extends React.Component
render()
const blackHeader = "background-color" : 'black'
const clearHeader = "background-color" : 'transparent'
return(
<header style= this.props.location.pathname === '/Contact' ? blackHeader : clearHeader >
<NavLink to='/'><h2>Name</h2></NavLink>
<nav>
<ul>
<li><NavLink to='/'>Portfolio</NavLink></li>
<li><NavLink to='Contact'>Contact</NavLink></li>
<li>Blog</li>
</ul>
</nav>
</header>
)
export default Header;
reactjs react-router
add a comment |
id like to change my header color when in my contact page. unable to get location from react router. I get "TypeError: Cannot read property 'pathname' of undefined".
I know I'm missing something simple but i cant seem to figure it out.
import React from 'react';
import './Header.css';
import NavLink from 'react-router-dom';
class Header extends React.Component
render()
const blackHeader = "background-color" : 'black'
const clearHeader = "background-color" : 'transparent'
return(
<header style= this.props.location.pathname === '/Contact' ? blackHeader : clearHeader >
<NavLink to='/'><h2>Name</h2></NavLink>
<nav>
<ul>
<li><NavLink to='/'>Portfolio</NavLink></li>
<li><NavLink to='Contact'>Contact</NavLink></li>
<li>Blog</li>
</ul>
</nav>
</header>
)
export default Header;
reactjs react-router
How are you using thisHeader
component in your app? Is it acomponent
prop to aRoute
component?
– Tholle
Nov 9 at 19:27
Possible duplicate of react router this.props.location
– Sterling Archer
Nov 9 at 19:29
@Tholle yes Header is in a Route component. got in working with withRouter, but still confused as to why i couldn't pass it as props though.
– Adam G
Nov 9 at 20:23
add a comment |
id like to change my header color when in my contact page. unable to get location from react router. I get "TypeError: Cannot read property 'pathname' of undefined".
I know I'm missing something simple but i cant seem to figure it out.
import React from 'react';
import './Header.css';
import NavLink from 'react-router-dom';
class Header extends React.Component
render()
const blackHeader = "background-color" : 'black'
const clearHeader = "background-color" : 'transparent'
return(
<header style= this.props.location.pathname === '/Contact' ? blackHeader : clearHeader >
<NavLink to='/'><h2>Name</h2></NavLink>
<nav>
<ul>
<li><NavLink to='/'>Portfolio</NavLink></li>
<li><NavLink to='Contact'>Contact</NavLink></li>
<li>Blog</li>
</ul>
</nav>
</header>
)
export default Header;
reactjs react-router
id like to change my header color when in my contact page. unable to get location from react router. I get "TypeError: Cannot read property 'pathname' of undefined".
I know I'm missing something simple but i cant seem to figure it out.
import React from 'react';
import './Header.css';
import NavLink from 'react-router-dom';
class Header extends React.Component
render()
const blackHeader = "background-color" : 'black'
const clearHeader = "background-color" : 'transparent'
return(
<header style= this.props.location.pathname === '/Contact' ? blackHeader : clearHeader >
<NavLink to='/'><h2>Name</h2></NavLink>
<nav>
<ul>
<li><NavLink to='/'>Portfolio</NavLink></li>
<li><NavLink to='Contact'>Contact</NavLink></li>
<li>Blog</li>
</ul>
</nav>
</header>
)
export default Header;
reactjs react-router
reactjs react-router
asked Nov 9 at 19:26
Adam G
72
72
How are you using thisHeader
component in your app? Is it acomponent
prop to aRoute
component?
– Tholle
Nov 9 at 19:27
Possible duplicate of react router this.props.location
– Sterling Archer
Nov 9 at 19:29
@Tholle yes Header is in a Route component. got in working with withRouter, but still confused as to why i couldn't pass it as props though.
– Adam G
Nov 9 at 20:23
add a comment |
How are you using thisHeader
component in your app? Is it acomponent
prop to aRoute
component?
– Tholle
Nov 9 at 19:27
Possible duplicate of react router this.props.location
– Sterling Archer
Nov 9 at 19:29
@Tholle yes Header is in a Route component. got in working with withRouter, but still confused as to why i couldn't pass it as props though.
– Adam G
Nov 9 at 20:23
How are you using this
Header
component in your app? Is it a component
prop to a Route
component?– Tholle
Nov 9 at 19:27
How are you using this
Header
component in your app? Is it a component
prop to a Route
component?– Tholle
Nov 9 at 19:27
Possible duplicate of react router this.props.location
– Sterling Archer
Nov 9 at 19:29
Possible duplicate of react router this.props.location
– Sterling Archer
Nov 9 at 19:29
@Tholle yes Header is in a Route component. got in working with withRouter, but still confused as to why i couldn't pass it as props though.
– Adam G
Nov 9 at 20:23
@Tholle yes Header is in a Route component. got in working with withRouter, but still confused as to why i couldn't pass it as props though.
– Adam G
Nov 9 at 20:23
add a comment |
2 Answers
2
active
oldest
votes
You need to pass the location props to your Header component or you can use the HOC withRouter in react-router-dom.
import NavLink, withRouter from 'react-router-dom';
export default withRouter(Header)
Edit for clarity:
If you render a component in your router in a Route component:
<Route path='/' component=Home />
This will give that component (Home in this case) access to the location props which can then be passed to children of that component.
import React, Component from 'react'
import OtherComponent from './OtherComponent'
class Home extends Component
render()
return (
<div>
<OtherComponent locations=this.props.locations
</div>
)
export default Home;
However, if the component is not passed through Route it will not have access to location props. For example, if you put your header outside of your routes, something like:
<Router>
<Header/>
<Switch>
<Route path='/' component=Home/>
</Switch>
</Router>
It will not have location props.
I'm not sure how your component tree is set up but, I hope this gives a little clarity to how the location props are accessed and used in your app.
The documentation is a good place to get some more clarity on the workings of react router as well https://reacttraining.com/react-router/core/api/Route/route-props
when i try to pass location props to my header with <Header location=this.props.location />i just get "location: undefined"
– Adam G
Nov 9 at 19:45
Do you have access to the location props in the parent? Try logging the props in the parent.
– seanulus
Nov 9 at 19:50
No. All i see is the_proto_ object? @seanulus
– Adam G
Nov 9 at 19:55
Alright. it's like @Tholle was saying above. If your component isn't rendered through a <Route/> component, you won't have access to the location props in said component. The withRouter higher order component should give you access to the location props as well, although, I'm not sure if it's bad practice to go wrapping all your components if you're able to pass props instead.
– seanulus
Nov 9 at 20:01
Thanks for your help. I got it working with withRouter as you said. But still curious why i couldn't pass location as props. Do you know why I wasn't able to?
– Adam G
Nov 9 at 20:21
|
show 1 more comment
Yea, I can see in the contact path
<li><NavLink to='Contact'>Contact</NavLink></li>
,
it should be something like
<li><NavLink to='/Contact'>Contact</NavLink></li>
.
You have to include the back slash, I mean this ' / ' before the pathname. So try it now.
My routes are working, but my problem is that i can't access the location props.
– Adam G
Nov 9 at 19:49
ok ..... @AdamG
– akolliy
Nov 12 at 10: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%2f53232138%2fnot-getting-location-react-router%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
You need to pass the location props to your Header component or you can use the HOC withRouter in react-router-dom.
import NavLink, withRouter from 'react-router-dom';
export default withRouter(Header)
Edit for clarity:
If you render a component in your router in a Route component:
<Route path='/' component=Home />
This will give that component (Home in this case) access to the location props which can then be passed to children of that component.
import React, Component from 'react'
import OtherComponent from './OtherComponent'
class Home extends Component
render()
return (
<div>
<OtherComponent locations=this.props.locations
</div>
)
export default Home;
However, if the component is not passed through Route it will not have access to location props. For example, if you put your header outside of your routes, something like:
<Router>
<Header/>
<Switch>
<Route path='/' component=Home/>
</Switch>
</Router>
It will not have location props.
I'm not sure how your component tree is set up but, I hope this gives a little clarity to how the location props are accessed and used in your app.
The documentation is a good place to get some more clarity on the workings of react router as well https://reacttraining.com/react-router/core/api/Route/route-props
when i try to pass location props to my header with <Header location=this.props.location />i just get "location: undefined"
– Adam G
Nov 9 at 19:45
Do you have access to the location props in the parent? Try logging the props in the parent.
– seanulus
Nov 9 at 19:50
No. All i see is the_proto_ object? @seanulus
– Adam G
Nov 9 at 19:55
Alright. it's like @Tholle was saying above. If your component isn't rendered through a <Route/> component, you won't have access to the location props in said component. The withRouter higher order component should give you access to the location props as well, although, I'm not sure if it's bad practice to go wrapping all your components if you're able to pass props instead.
– seanulus
Nov 9 at 20:01
Thanks for your help. I got it working with withRouter as you said. But still curious why i couldn't pass location as props. Do you know why I wasn't able to?
– Adam G
Nov 9 at 20:21
|
show 1 more comment
You need to pass the location props to your Header component or you can use the HOC withRouter in react-router-dom.
import NavLink, withRouter from 'react-router-dom';
export default withRouter(Header)
Edit for clarity:
If you render a component in your router in a Route component:
<Route path='/' component=Home />
This will give that component (Home in this case) access to the location props which can then be passed to children of that component.
import React, Component from 'react'
import OtherComponent from './OtherComponent'
class Home extends Component
render()
return (
<div>
<OtherComponent locations=this.props.locations
</div>
)
export default Home;
However, if the component is not passed through Route it will not have access to location props. For example, if you put your header outside of your routes, something like:
<Router>
<Header/>
<Switch>
<Route path='/' component=Home/>
</Switch>
</Router>
It will not have location props.
I'm not sure how your component tree is set up but, I hope this gives a little clarity to how the location props are accessed and used in your app.
The documentation is a good place to get some more clarity on the workings of react router as well https://reacttraining.com/react-router/core/api/Route/route-props
when i try to pass location props to my header with <Header location=this.props.location />i just get "location: undefined"
– Adam G
Nov 9 at 19:45
Do you have access to the location props in the parent? Try logging the props in the parent.
– seanulus
Nov 9 at 19:50
No. All i see is the_proto_ object? @seanulus
– Adam G
Nov 9 at 19:55
Alright. it's like @Tholle was saying above. If your component isn't rendered through a <Route/> component, you won't have access to the location props in said component. The withRouter higher order component should give you access to the location props as well, although, I'm not sure if it's bad practice to go wrapping all your components if you're able to pass props instead.
– seanulus
Nov 9 at 20:01
Thanks for your help. I got it working with withRouter as you said. But still curious why i couldn't pass location as props. Do you know why I wasn't able to?
– Adam G
Nov 9 at 20:21
|
show 1 more comment
You need to pass the location props to your Header component or you can use the HOC withRouter in react-router-dom.
import NavLink, withRouter from 'react-router-dom';
export default withRouter(Header)
Edit for clarity:
If you render a component in your router in a Route component:
<Route path='/' component=Home />
This will give that component (Home in this case) access to the location props which can then be passed to children of that component.
import React, Component from 'react'
import OtherComponent from './OtherComponent'
class Home extends Component
render()
return (
<div>
<OtherComponent locations=this.props.locations
</div>
)
export default Home;
However, if the component is not passed through Route it will not have access to location props. For example, if you put your header outside of your routes, something like:
<Router>
<Header/>
<Switch>
<Route path='/' component=Home/>
</Switch>
</Router>
It will not have location props.
I'm not sure how your component tree is set up but, I hope this gives a little clarity to how the location props are accessed and used in your app.
The documentation is a good place to get some more clarity on the workings of react router as well https://reacttraining.com/react-router/core/api/Route/route-props
You need to pass the location props to your Header component or you can use the HOC withRouter in react-router-dom.
import NavLink, withRouter from 'react-router-dom';
export default withRouter(Header)
Edit for clarity:
If you render a component in your router in a Route component:
<Route path='/' component=Home />
This will give that component (Home in this case) access to the location props which can then be passed to children of that component.
import React, Component from 'react'
import OtherComponent from './OtherComponent'
class Home extends Component
render()
return (
<div>
<OtherComponent locations=this.props.locations
</div>
)
export default Home;
However, if the component is not passed through Route it will not have access to location props. For example, if you put your header outside of your routes, something like:
<Router>
<Header/>
<Switch>
<Route path='/' component=Home/>
</Switch>
</Router>
It will not have location props.
I'm not sure how your component tree is set up but, I hope this gives a little clarity to how the location props are accessed and used in your app.
The documentation is a good place to get some more clarity on the workings of react router as well https://reacttraining.com/react-router/core/api/Route/route-props
edited Nov 9 at 20:55
answered Nov 9 at 19:34
seanulus
2243
2243
when i try to pass location props to my header with <Header location=this.props.location />i just get "location: undefined"
– Adam G
Nov 9 at 19:45
Do you have access to the location props in the parent? Try logging the props in the parent.
– seanulus
Nov 9 at 19:50
No. All i see is the_proto_ object? @seanulus
– Adam G
Nov 9 at 19:55
Alright. it's like @Tholle was saying above. If your component isn't rendered through a <Route/> component, you won't have access to the location props in said component. The withRouter higher order component should give you access to the location props as well, although, I'm not sure if it's bad practice to go wrapping all your components if you're able to pass props instead.
– seanulus
Nov 9 at 20:01
Thanks for your help. I got it working with withRouter as you said. But still curious why i couldn't pass location as props. Do you know why I wasn't able to?
– Adam G
Nov 9 at 20:21
|
show 1 more comment
when i try to pass location props to my header with <Header location=this.props.location />i just get "location: undefined"
– Adam G
Nov 9 at 19:45
Do you have access to the location props in the parent? Try logging the props in the parent.
– seanulus
Nov 9 at 19:50
No. All i see is the_proto_ object? @seanulus
– Adam G
Nov 9 at 19:55
Alright. it's like @Tholle was saying above. If your component isn't rendered through a <Route/> component, you won't have access to the location props in said component. The withRouter higher order component should give you access to the location props as well, although, I'm not sure if it's bad practice to go wrapping all your components if you're able to pass props instead.
– seanulus
Nov 9 at 20:01
Thanks for your help. I got it working with withRouter as you said. But still curious why i couldn't pass location as props. Do you know why I wasn't able to?
– Adam G
Nov 9 at 20:21
when i try to pass location props to my header with <Header location=this.props.location />i just get "location: undefined"
– Adam G
Nov 9 at 19:45
when i try to pass location props to my header with <Header location=this.props.location />i just get "location: undefined"
– Adam G
Nov 9 at 19:45
Do you have access to the location props in the parent? Try logging the props in the parent.
– seanulus
Nov 9 at 19:50
Do you have access to the location props in the parent? Try logging the props in the parent.
– seanulus
Nov 9 at 19:50
No. All i see is the_proto_ object? @seanulus
– Adam G
Nov 9 at 19:55
No. All i see is the_proto_ object? @seanulus
– Adam G
Nov 9 at 19:55
Alright. it's like @Tholle was saying above. If your component isn't rendered through a <Route/> component, you won't have access to the location props in said component. The withRouter higher order component should give you access to the location props as well, although, I'm not sure if it's bad practice to go wrapping all your components if you're able to pass props instead.
– seanulus
Nov 9 at 20:01
Alright. it's like @Tholle was saying above. If your component isn't rendered through a <Route/> component, you won't have access to the location props in said component. The withRouter higher order component should give you access to the location props as well, although, I'm not sure if it's bad practice to go wrapping all your components if you're able to pass props instead.
– seanulus
Nov 9 at 20:01
Thanks for your help. I got it working with withRouter as you said. But still curious why i couldn't pass location as props. Do you know why I wasn't able to?
– Adam G
Nov 9 at 20:21
Thanks for your help. I got it working with withRouter as you said. But still curious why i couldn't pass location as props. Do you know why I wasn't able to?
– Adam G
Nov 9 at 20:21
|
show 1 more comment
Yea, I can see in the contact path
<li><NavLink to='Contact'>Contact</NavLink></li>
,
it should be something like
<li><NavLink to='/Contact'>Contact</NavLink></li>
.
You have to include the back slash, I mean this ' / ' before the pathname. So try it now.
My routes are working, but my problem is that i can't access the location props.
– Adam G
Nov 9 at 19:49
ok ..... @AdamG
– akolliy
Nov 12 at 10:22
add a comment |
Yea, I can see in the contact path
<li><NavLink to='Contact'>Contact</NavLink></li>
,
it should be something like
<li><NavLink to='/Contact'>Contact</NavLink></li>
.
You have to include the back slash, I mean this ' / ' before the pathname. So try it now.
My routes are working, but my problem is that i can't access the location props.
– Adam G
Nov 9 at 19:49
ok ..... @AdamG
– akolliy
Nov 12 at 10:22
add a comment |
Yea, I can see in the contact path
<li><NavLink to='Contact'>Contact</NavLink></li>
,
it should be something like
<li><NavLink to='/Contact'>Contact</NavLink></li>
.
You have to include the back slash, I mean this ' / ' before the pathname. So try it now.
Yea, I can see in the contact path
<li><NavLink to='Contact'>Contact</NavLink></li>
,
it should be something like
<li><NavLink to='/Contact'>Contact</NavLink></li>
.
You have to include the back slash, I mean this ' / ' before the pathname. So try it now.
edited Nov 9 at 20:05
answered Nov 9 at 19:36
akolliy
145
145
My routes are working, but my problem is that i can't access the location props.
– Adam G
Nov 9 at 19:49
ok ..... @AdamG
– akolliy
Nov 12 at 10:22
add a comment |
My routes are working, but my problem is that i can't access the location props.
– Adam G
Nov 9 at 19:49
ok ..... @AdamG
– akolliy
Nov 12 at 10:22
My routes are working, but my problem is that i can't access the location props.
– Adam G
Nov 9 at 19:49
My routes are working, but my problem is that i can't access the location props.
– Adam G
Nov 9 at 19:49
ok ..... @AdamG
– akolliy
Nov 12 at 10:22
ok ..... @AdamG
– akolliy
Nov 12 at 10: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%2f53232138%2fnot-getting-location-react-router%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
How are you using this
Header
component in your app? Is it acomponent
prop to aRoute
component?– Tholle
Nov 9 at 19:27
Possible duplicate of react router this.props.location
– Sterling Archer
Nov 9 at 19:29
@Tholle yes Header is in a Route component. got in working with withRouter, but still confused as to why i couldn't pass it as props though.
– Adam G
Nov 9 at 20:23