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
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.
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.
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>
))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With