0

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?

3
  • Why don't you disable the lint rule for this function? Destructuring here reduces readability. What are you mapping? What does this function take? Commented Mar 22, 2022 at 19:16
  • Could you share your eslintrc? Take a look at the rule here github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/… Commented Mar 22, 2022 at 19:16
  • it's a shared project and I don't have a say in the enforced linting rules. If there isn't a way around it I will try to figure out how to rearrange my code. Commented Mar 22, 2022 at 19:58

3 Answers 3

1

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'}
        ])
      }
      
    </>
  );
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

I am doing that in my original code. I actually only unpacked name afterward hoping that that is the issue the lint error was pointing out but no such luck. It is complaining about files not being deconstructed. I know what map does. I only tried the second method to see if it made the lint error go away and hoped against hope that it would unpack it as the map function attached to the array prototype but doesn't seem to be the case.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.