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
React - Object field as dependency of useMemo

I have the following code:

   const iconMap = useMemo(() => ({
     HomeStacks: {
       iconName: "home",
       iconType: "material-community",
     },
     ActivityStacks: {
       iconName: "bell",
       iconType: "material-community",
       badgeCount: badges["activity"],
     },
   }), [badges["activity"]]);

And, for some reason, ESLint is throwing me an error from the rule "react-hooks/exhaustive-deps":

React Hook useMemo has a missing dependency: 'badges'. Either include it or remove the dependency array. eslintreact-hooks/exhaustive-deps

So, is it not valid to use object fields as dependencies?

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

0

It definitely is, but eslint appears to be confused by the bracket notation accessor. In newer versions of react-hooks/exhaustive-deps it is perfectly fine to use dot notation in the dependency list, but for any use cases where it will complain, you can always extract the property to a constant above the dependency list like so (assuming you do not want to suppress the warning):

 const activity = badges["activity"];
 const iconMap = useMemo(() => ({
     HomeStacks: {
       iconName: "home",
       iconType: "material-community",
     },
     ActivityStacks: {
       iconName: "bell",
       iconType: "material-community",
       badgeCount: activity,
     },
   }), [activity]);
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!