Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!