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

140
Views
warning: React Hook useEffect has a missing dependency

how can i get rid of the warning "React Hook useEffect has a missing dependency" while writing my code ?

This is a part of the code and the warning i get

useEffect(() => {
        if(inactive){
            document.querySelectorAll('.sub-menu').forEach((el) => {
                el.classList.remove("active");
            });
                    }

                    
        props.onCollapse(inactive);
    }, [inactive]);

enter image description here

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to include props in dependency array.

useEffect(() => {
// your code
}, [props, inactive]);
about 4 years ago · Juan Pablo Isaza Report

0

You should include props.onCollapse as a dependency.

useEffect(() => {
  // ...
}, [props.onCollapse, inactive]) // ✅

You should not pass the entire props object as another answer suggests.

And there is something else happening in your code that also needs attention. See how you are using document.querySelector and classList remove?

 document.querySelectorAll('.sub-menu').forEach((el) => {
  el.classList.remove("active"); // ⚠️ react is not jQuery
});

This is an anti-pattern in react. Instead your Menu component should render submenu classNames as a pure function of the menu state.

function Menu({ items = [], initState = true }) {
  const [active, setActive] = React.useState(initState)
  const onClick = event => { event.stopPropagation(); setActive(!active) }
  return active // ✅ data-driven render
    ? <div className="menu" onClick={onClick}>
        {items.map((item, key) =>
          Array.isArray(item)
            ? <Menu key={key} items={item} />
            : <MenuItem key={key} item={item} />
        )}
      </div>
    : <div className="menu" onClick={onClick}>
        <MenuItem item={items[0]} />
      </div>
}

Run the code below and click some menu items to see them toggle open and closed. Notice useEffect, document.query* and classList effects are not needed.

function Menu({ items = [], initState = true }) {
  const [active, setActive] = React.useState(initState)
  const onClick = event => { event.stopPropagation(); setActive(!active) }
  return active
    ? <div className="menu" onClick={onClick}>
        {items.map((item, key) =>
          Array.isArray(item)
            ? <Menu key={key} items={item} />
            : <MenuItem key={key} item={item} />
        )}
      </div>
    : <div className="menu" onClick={onClick}>
        <MenuItem item={items[0]} />
      </div>
}

function MenuItem({ item }) {
  return <div>{item}</div>
}

const mymenu = [ "Main Menu",
  ["Clothing", "Shirts", "Pants", ["Footwear", "Shoes", "Boots"]],
  ["Accessories", "Bags", "Hats"]
]

ReactDOM.render(<Menu items={mymenu} />, document.querySelector("#app"))
.menu {
  border: 1px solid gray;
  padding: 1rem;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.14.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.14.0/umd/react-dom.production.min.js"></script>
<div id="app"></div>

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!