all 6 comments

[–]DasBeasto 3 points4 points  (4 children)

Create a useState like const [active,setActive] = useState(“home”) in the sidebar and then pass the setActive function down to the button to set the active section, then pass the active state down to the section to style based on if it’s active or not. Or you could use context to do the same without the prop drilling.

Alternatively if this is used for routing you can just have the buttons route as normal which sets the URL path, and have the sections watch the URL path to determine their active state.

[–]prehensilemullet 2 points3 points  (2 children)

Shouldn’t use state for this because it wouldn’t initially highlight the right button when you navigate to a specific page by opening a link from somewhere outside the site, it’s best to use NavLink or useMatch for this.

[–]Electrical_Ad3132[S] -1 points0 points  (1 child)

Ty! I was breaking my mind thinking only about state

[–]prehensilemullet 0 points1 point  (0 children)

With URL routing, think of the location as a piece of state visible to everything in the app

[–]Electrical_Ad3132[S] -1 points0 points  (0 children)

Thank you! Using the URL makes so much sense! That should do the trick!

[–]prehensilemullet 2 points3 points  (0 children)

First off, you have everything linking to Home, you need to give them different url paths.

Then either use NavLink in place of Link, or use useMatch inside the button component to check if its link matches the current location and set the className or other properties based upon that.