I'd like to import different CSS depending on which page I'm on. The HTML will stay the same. What do I need to do to access the current location?
I was thinking I could put an if
statement in the constructor and import the CSS there, but I'm not sure that will work.
EDIT: you can't import from within the constructor
, so is there a way to do a conditional import?
Here's my code:
import React from 'react'
import Link from 'gatsby-link'
class Menu extends React.Component {
constructor(props) {
super(props)
//how do I get myCurrentLocation?
if(myCurrentLocation == '/') {
import menuStyle from '../page-assets/global/styles/menu/_home-page.sass'
} else {
import menuStyle from '../page-assets/global/styles/menu/_regular-page.sass'
}
}
render (){
return (
<nav className="menu">
<Link to="/work-ive-done/" className="menu-item">
<span className="menu-item__heading">
Work
</span>
<span className="menu-item__sub-text">
ive done
</span>
</Link>
//other menu items ...
</nav>
)
}
}
export default Menu;