Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
React Newbie Question - How do I make a parameter true depending on the path?

I have a parameter called 'selected' that highlights the button on the sidebar in css. The tutorial I followed unfortunately didn't show how to change the state of this parameter to either true or false. I'd like to know how to do this for future projects.

import { Update } from '@mui/icons-material';
import React from 'react'
import { Link } from 'react-router-dom';
import './SidebarRow.css'

function SidebarRow({ selected, title, Icon, linkto }) {
    
    if (title == "Home" && (window.location.pathname == "/"))
    {
        selected = true;
    }
    else if (title == "Trending" && (window.location.pathname == "/trending"))
    {
        selected = true;
    }
    else{
        selected=false;
    }


    return (
        <Link to={`/${linkto}`} >
        <div className= {`sidebarRow ${selected && "selected"}`} >
            <Icon className="sidebarRow_icon"/>
            <h2 className="sidebarRow_title">{title}</h2>
            
        </div>
        </Link>
    )
}

export default SidebarRow;

The 'if' statements are my beginner take on this problem, and it doesn't work correctly. Any help would be much appreciated.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The main issue in your logic is that you are passing selected as a props but then want to change its value. A props should not be modified. Either define the proper value for selected in your parent component and pass it down to the child component, or create a new variable such as :

let localSelected = (title == "Home" && (window.location.pathname == "/") || (title == "Trending" && (window.location.pathname == "/trending") ? true : false ;

You can keep the if logic but ternary operator seems much cleaner.

about 4 years ago · Juan Pablo Isaza Denunciar

0

My recommendation is to use React Router V6 and <NavLink> as a special version of the <Link> that will add styling attributes to the rendered element when it matches the current URL.

A <NavLink> is a special kind of <Link> that knows whether or not it is "active".

https://reactrouter.com/docs/en/v6/api#navlink

about 4 years ago · Juan Pablo Isaza Denunciar

0

I would suggest you, apart from the previous answer, reading about NavLink component from React Router. I think most of the logic you need is included on it.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda