score:3
Accepted answer
For your current setup, just pass in an active
prop to your header from each component:
const Dashboard = React.createClass({
render() {
return <div>
<Nav active="dashboard" />
</div>
}
})
const About = React.createClass({
render() {
return <div>
<Nav active="about" />
</div>
}
})
const Messages = React.createClass({
render() {
return <div>
<Nav active="messages" />
// other stuff
</div>
}
})
const Nav = React.createClass({
isActive(value){
return 'nav-link '+((value===this.props.active) ?'active':'');
},
render() {
return <div>
<Link className={this.isActive("settings")}>Settings</Link>
<Link className={this.isActive("dashboard")}>Dashboard</Link>
</div>
}
})
render((
<Router>
<Route path="/" component={App}>
<IndexRoute component={Dashboard} />
<Route path="about" component={About} />
<Route path="inbox" component={Inbox}>
<Route path="messages/:id" component={Messages} />
</Route>
</Route>
</Router>
), document.body)
Source: stackoverflow.com
Related Query
- React — have active item state stay with menu component when loaded into new page
- React Js / Active item on menu with state
- React doesn't re-render component with new props when state changes
- React Js / Menu with menu item drop down and menu item active State
- Accessing the State of a Functional Component with React Hooks when Testing with Enzyme
- How to access state when component unmount with React Hooks?
- In Webpack 4, can we dynamically generate page chunk with import() token so we can turn a react component into a react-loadable one?
- React router - pass api data to the linked component to open with a new page
- How to maintain previous changed state in REACT JS as when new item is updated, previous is set to default?
- React components not rendering when routing to new page with react-router-dom v6
- How can I update component in React after adding a new item into the database, using react hooks?
- Embedded child component doesn't get changed when I click a new one with React Router
- How do I create a class decorator for mixing new methods into a React component when using Typescript?
- I have this `div` on my home page and want it to move with me when I scroll, I am using react for the website btw
- Embed a single react component created with create-react-library into html page
- How can I make react render the new state modifications after I have updated the state with hooks and context
- Typing component state with typescript when using react hooks
- React component (textarea) won't re-render with new style from updated state
- I'm working with React Route, and I have a random string, but when I do click on the link page works but when a do click again doesn't work
- React component create a new instance of the state when re-rendering even the state is not changed
- React component not rerendring when state is changed with Redux
- React component is not getting updated when state changes into the redux
- Functional component event is still active when state is toggled - REACT MAPBOX
- React list element disappears in grandchild component when grandparent state is updated. Warning: Each child in a list should have a unique "key"
- React doesn't re-render component after state changes when main thread is queued up with other operations?
- Can not able to get the location state when opened with "open link in a new tab" in react router
- How to set state of a react component with a specific item from a returned json object?
- React component not refreshing when redux state has been updated with successful action
- Got an error when tried to add new element into array state in React
- React not registering state change when setting previous state with slight modifications as the new one
More Query from same tag
- How can I exclude a source named test.js from being detected to be test file in a create react app?
- How to change the color of the primary Material UI
- How to call API only once with React Query?
- Prevent infinite rerender of component custom hook
- Redux-Saga and createAction from Redux-Toolkit does not run
- Display message for n seconds after AJAX response
- Trying to install webpack 5 with react 17 for module federation, getting this error on npm install
- React search by name
- ReactJS and Less, set dynamic class
- Why state returned from my reducer is an empty object??(React-Redux)
- Spread operator redorders array alphabetical
- passing and displaying array of objects in react js
- javascript/ReactJS: Show results from backend in a list
- Issue with history.push
- ReactJS - onClick SweetAlert is not working
- Styling SVG as a React Component
- Uncaught TypeError: Cannot read property 'type' of undefined at start of switch function
- Next.JS & JEST - publicRuntimeConfig undefined
- How can I replace bits or string with React component, react-router-dom Link or an anchor tag <a></a>
- React Router not routing correctly
- why leaflet map doesn't render on react app
- React + Redux: Uncaught Error: Expected the reducer to be a function
- React js update child state from an event in the parent
- Why am I directed to the same page when using routes in react js
- How to get a response from express server about an error using axios in react app
- Sticky navbar not updating
- IE11 React.memo + react-redux connect issue
- I want an error message if the entered username is not present on github
- React Todo Delete Button Removes all listed items at once
- Missing return type on a function which returns a React Fragment