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

255
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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