Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

React Planet Fact Site w/ React Router

@LogvnR

Desktop design screenshot for the Planets fact site coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
3intermediate
View challenge

Design comparison


SolutionDesign

Solution retrospective


How could I refine the State Management for the Desktop NavBar?

Community feedback

Alex Li 400

@awexli

Posted

Hey Logan, great question.

A good starting place is to improve your component by mapping (looping) <DesktopNavBarPlanets /> instead manually of creating one for each planet. This way, you are not repeating code.

For example:

{planets.map((planet) => {
    return (
        <DesktopNavBarPlanets
            ...
            planet={planet.name}
        />
    );
})}

For state management, you can use one state that takes in the name of the planet, so a string instead of a boolean.

For example:

const [selectedPlanet, setSelectedPlanet] = useState(planets[0].name)

In the <DesktopNavBarPlanets /> component you can then check if the selectedPlanet matches with props.planet. If it matches style the border, otherwise leave it transparent.

For example:

style={
    props.selectedPlanet === props.planet
        ? { borderTopColor: `var(--${props.planet}-theme)` }
        : { borderBottomColor: 'transparent' }
}

As for changing state onClick, you’ll have to update the selectedPlanet state. I’ll leave that to you to figure out :-)

Marked as helpful

1

@LogvnR

Posted

@awexli Hey thanks a lot for that suggestion! I have gone back and refactored that section and it is what I wanted to achieve, to begin with!

I had originally used .map() on the desktop navbar, as it is what I did for the mobile version, but I couldn't get the color on the top of the tabs to change when each one was clicked. I had never thought about using state and then comparing the state to the planet name to apply the styles that way. Definitely, something I'll remember for the future.

Again, thanks for taking the time to offer a suggestion!

1

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord