react eslint错误中未使用的状态字段



新的反应,我使用一个类组件,并得到未使用的状态字段错误为filteredItems

我不明白为什么我已经在datatable组件中使用它们了

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

这个错误的意思正是它所说的:你的状态有字段[s],没有读取任何地方。如果你初始化state={ attributeA: '', attributeB: ''}而不读取attributeA,那么你会得到这个错误。

你的例子有很多缺失的代码。然而,在您的示例中,您不使用state.filterText,这就是错误的来源。

请记住,你得到的错误不是来自javascript或React本身,而是来自eslint,这是一个用于突出错误实践的工具(例如声明状态属性而不使用它们)。

最新更新