user8779054
user8779054

Reputation: 183

Unused state fields in react eslint error

New to react, I'm using a class component and getting unused state fields error for filteredItems

Don't understand why is it unused when i'm already using them in my datatable component

  constructor(props) {
    super(props);
    this.state = {
      filterText: '',
      filteredItems: '', //Initalize state
    };
  }

 // ... ommitted other parts

  render() {
    const result = arr1.map(item => item[1]);
    // this.filteredItems = result;
    if (result) {
      this.setState({
        filteredItems: result,  //Trying to setState with result array. Getting unused state fields error
      });
    }

    return (
      <React.Fragment>
        <DataTable
          columns={columns}
          data={this.filteredItems} // Trying to Using this.filteredItems

    // ... ommitted other parts

Upvotes: 0

Views: 717

Answers (1)

juliomrc
juliomrc

Reputation: 626

The error means exactly what it says: Your state has field[s] that are not read anywhere. If you initialize state={ attributeA: '', attributeB: ''} and never read attributeA, then you will get that error.

Your example has a lot of missing code. However, in your example you do not use the state.filterText and that is where the error comes from.

Keep in mind that the error you are getting is not from javascsript or React themselves, but from eslint, which is a tool used to hightlight wrong practices (such as declaring state attributes and not using them).

Upvotes: 1

Related Questions