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

94
Visualizações
Click events in body in React JS

I made a react component which adds a border when clicked. Now I have 3 similar components on the same page. When I click anyone of them,it adds a border. When I click on any of the other remaining components I want the border to be removed from first and added to the second. Also if I click anywhere in the document other than the components, existing border if any must be removed. I have achieved the same using vanilla Js but unable to implement it using react. Any help is appreciated.

import React from 'react'

function NavItem() {
    const menu = {
        initial: "noborder",
        final: "border",
      };
      const [itemStyle, setItemStyle] = useState("");
    const handleClick=()=>{
        setItemStyle(final)
    }
  return (
    <div onClick={handleClick} className={`${itemStyle}`}>NavItem</div>
  )
}

export default NavItem
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Lift the state up to a parent component. Pass the selected state down to each child component. Use an effect hook to wire up a global event handler to catch clicks elsewhere in the document.

const Parent = () => {
  const [selected, setSelected] = React.useState(null);
  console.log(selected);
  React.useEffect(() => {
    const clearSelected = () => setSelected(null);
    window.addEventListener("click", clearSelected);
    return () => window.removeEventListener("click", clearSelected);
  }, []);

  const values = ["a", "b", "c"];

  return (
    <div>
      {values.map((value) => (
        <Child
          key={value}
          value={value}
          selected={value === selected}
          setSelected={setSelected}
        />
      ))}
    </div>
  );
};

const Child = ({ value, selected, setSelected }) => {
  console.log({ setSelected, selected, value });
  return (
    <button
      type="button"
      className={selected ? "selected" : "not-selected"}
      onClick={(e) => {
        e.stopPropagation();
        setSelected(value);
      }}
    >
      {value}
    </button>
  );
};

ReactDOM.render(<Parent />, document.getElementById("root"));
about 4 years ago · Juan Pablo Isaza Relatório

0

Your problem is you call setState separately for each NavItem, and you don't have the common parent component to share that state, so you should lift your states to the parent component

I assume that you have a parent component called Nav

import React from 'react'
import NavItem from './NavItem'

function Nav() {
    const menu = {
        initial: "noborder",
        final: "border",
      };
    const [activeNavItem, setActiveNavItem] = useState(1); //active the first `NavItem`
    //you can reduce duplication by `map` or depends on your setup
    return <>
       <NavItem itemStyle={activeNavItem === 1 ? menu.final : menu.initial } handleClick={() => setActiveNavItem(1)}>Item 1</NavItem>
       <NavItem itemStyle={activeNavItem === 2 ? menu.final : menu.initial } handleClick={() => setActiveNavItem(2)}>Item 2</NavItem>
       <NavItem itemStyle={activeNavItem === 3 ? menu.final : menu.initial } handleClick={() => setActiveNavItem(3)}>Item 3</NavItem>
    </>
}

export default Nav

Now you have the same state for all NavItem

import React from 'react'

function NavItem({ itemStyle, handleClick }) {
  return (
    <div onClick={handleClick} className={`${itemStyle}`}>NavItem</div>
  )
}

export default NavItem
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