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

247
Views
Eslint error went passing array into function and using map on it

I am getting the linting error

Must use destructuring files assignment  react/destructuring-assignment

For the code below

const showFiles = label => (files) =>  
  (   
    <>
      {files.map(({ name }, index) => (
        <Typography key={`${label}-file-${index}`}>{name}</Typography>     
      ))}
    </>
  );

I tried changing it to this

const showFiles = label => ({ map }) =>
  (
    <>
      {map(({ name }, index) => (
        <Typography key={`${label}-file-${index}`}>{name}</Typography>
      ))}
    </>
  );

This makes the linting error go away but then the actual webpage has the following error.

TypeError: can't convert undefined to object

Is there a way around this linting error that I am not seeing? Do I have to use Array.prototypes.map or something?

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

0

The rest parameters and spread operators in ES6 would be a great use in this case. In the function showFiles, changing the files to ...files implies the function is expecting an array type argument, which improves readability and linting process.

For example

const showFiles = label => (...files) => {
    return (   
      <>
      {files.map(({ name }, index) => (
        <Typography key={`${label}-file-${index}`}>{name}</Typography>     
      ))}
    </>
    );
  }

And add the spread operators when calling the function

  return (
    <>
      {
        showFiles('fileLabel')(...[
         {name:'a'},
         {name:'b'}
        ])
      }
      
    </>
  );
about 4 years ago · Juan Pablo Isaza Report

0

I ended up changing it to

const showFiles = label => field => (
  <>
    {Array.prototype.map.call(field, ({ name }, index) => (
      <Typography key={`${label}-file-${index}`}>{name}</Typography>
    ))}
  </>
)

It works and the linting error is gone.

about 4 years ago · Juan Pablo Isaza Report

0

The map method want to iterate over each element of your array. So let your callback function be :

files.map(item => <Typography>{item.name}</Typography>)

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!