not getting location react router?










0














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;









share|improve this question





















  • 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










  • @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















0














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;









share|improve this question





















  • 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










  • @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













0












0








0







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;









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 19:26









Adam G

72




72











  • 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










  • @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










  • 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












2 Answers
2






active

oldest

votes


















-1














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






share|improve this answer






















  • 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


















-1














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.






share|improve this answer






















  • 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










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%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









-1














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






share|improve this answer






















  • 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















-1














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






share|improve this answer






















  • 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













-1












-1








-1






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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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













-1














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.






share|improve this answer






















  • 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















-1














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.






share|improve this answer






















  • 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













-1












-1








-1






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















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%2f53232138%2fnot-getting-location-react-router%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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)