Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

265
Visualizações
How to change the menu item's color of sidebar when we click on it with css in React pls

I'm new to the React, my objective is to show color when I click on one of the Menu Item in Sidebar. Can anyone assist me in how to change color while clicking the menu item?

How to change the menu item's color of sidebar when we click on it with Material-UI in React? I referenced the post but it didn't solve my problem.

https://i.stack.imgur.com/gInE5.png

Here is the Code:

import React from "react";
import { Collapse, List, ListItem } from "@material-ui/core";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
import ExpandLessIcon from "@material-ui/icons/ExpandLess";
import { useNavigate } from "react-router-dom";

const SidebarItem = ({ item }) => {

  const [collapsed, setCollapsed] = React.useState(true);
  const { title, enable, path, items } = item;
  let navigate = useNavigate();

  const toggleCollapse = () => {
    setCollapsed(prevValue => !prevValue); 
  }

  const onClick = () => {
    if (Array.isArray(items)) {
      console.log(items); 
      toggleCollapse();
    } else if (enable === true) {
      console.log(path);
      if (item[0]) {
        return navigate('');
      } else {
        return navigate("/setup/"+path);
      }
    }
  }

  let expandIcon;
  if (Array.isArray(items) && items.length) {
    expandIcon = !collapsed ? <ExpandLessIcon /> : <ExpandMoreIcon />;
  }

  return (
    <>
      <ListItem onClick={onClick} button dense>
        <div>{title}</div>
        {expandIcon}
      </ListItem>
      
      <Collapse in={!collapsed} timeout="auto" unmountOnExit>
        {Array.isArray(items) ? (
          <List>
            {items && items.map((subItem, index) => (
              <SidebarItem
                key={`${subItem}${index}`} 
                item={subItem}/>
            ))}
          </List>
        ) : null}
      </Collapse>
    </>
  );
}

const Sidebar = ({ items }) => {

  return (
    <>
      <List>
        {items && items.map((sidebarItem, index) => (
          <SidebarItem 
            key={`${sidebarItem}${index}`}
            item={sidebarItem}/>
        ))}
      </List>
    </>
  );
}

export default Sidebar;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can put a onClick function on sidebarItem which will toggle between true and false. whenever the value is true it will add class to your sidebar item. Try the code below.

 const [toggle, setToggle] = useState(false)
 const onClick = () => {
    setToggle(true)  
 }

 <List>
        {items && items.map((subItem, index) => (
          <SidebarItem
            key={`${subItem}${index}`} 
            item={subItem}/>
            className={`${toggle} ? 'className1' : ''`}
            onClick={onCLick}
        ))}
</List>
 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda