Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint Must use destructuring state assignment

Tags:

reactjs

eslint

I am getting ESlint error for the following line this.state.items.map(item => (

The error is Must use destructuring state assignment

{
            this.state.items.map(item => (
              <div key={item}>
                {
                item.links.map(thing => (
                  <NavLink
                    key={thing.link.id}
                    exact
                    to={thing.link.url}
                  >
                    {thing.link.text}
                  </NavLink>
                ))
                }
              </div>
            ))
          }

I am using "eslint-config-airbnb"

Also, this is my componentDidMount function if this is related

componentDidMount() {
  fetch('./data/data_arr.js')
  .then(results => results.json())
  .then(results => this.setState({ items: results }));
}

Any help to try and understand this would be great. thank you

like image 383
roshambo Avatar asked Oct 04 '18 03:10

roshambo


People also ask

What is Destructuring state assignment?

In simple terms, the destructuring assignment is a way to customize how a value is accessed for use. It is a JavaScript expression that allows the extraction of values from an array or properties from an object and assigns the value or property as a distinct variable.

What is Destructuring props assignment?

In destructuring, It does not change an array or any object, it makes a copy of the desired object or array element by assigning them in its own new variables, later we can use this new variable in React (class or functional) components.


1 Answers

That's called:

Enforce consistent usage of destructuring assignment of props, state, and context (react/destructuring-assignment)

More details are available here: destructuring-assignment

In order to make that warning/error disappear, you could do like this:

      ...
      const { items }= this.state;
      ...
      {
        items.map(item => (
          <div key={item}>
            {
            item.links.map(thing => (
              <NavLink
                key={thing.link.id}
                exact
                to={thing.link.url}
              >
                {thing.link.text}
              </NavLink>
            ))
            }
          </div>
        ))
      }
like image 99
You Nguyen Avatar answered Oct 20 '22 09:10

You Nguyen