[英]Compile warning no-unused-vars for vars that are used
So I have a React component, SearchBooks
that is receiving an array books
of from the main App.js
state as well as a function, onUpdateBooksState
, also from App.js
. 所以我有一个React组件
SearchBooks
,它从App.js
的主要状态接收数组books
,还有一个函数onUpdateBooksState
,也从App.js
。
Everything works fine but I am getting warnings saying neither are being used, but they are. 一切正常,但是我收到警告,说这两个都没有被使用,但是它们正在使用。
Why am I getting these warnings and how can I correct the underlying issue? 为什么会收到这些警告,如何解决根本问题?
SearchBooks.js
import React, { Component } from 'react'
import * as BooksAPI from './utils/BooksAPI'
import Book from './Book';
export default class SearchBooks extends Component {
state = {
query: '',
results: []
}
updateQuery(query) {
this.setState(() => ({
results: [],
query: query
}))
this.bookSearch(query)
}
bookSearch(e) {
if (e.length > 0)
BooksAPI.search(e)
.then(searchResults => this.setState(currentState => ({
results: this.updateExistingShelves(searchResults)
})));
}
updateExistingShelves(searchResults) {
if(searchResults.error !== "empty query") {
const myBooks = this.props.books // books used here
const addToState = searchResults.filter((result) => myBooks.find(b => {
if(b.id === result.id)
result.shelf = b.shelf
return result
}))
myBooks.concat(addToState)
return searchResults
}
}
render() {
const { query, results } = this.state
const { onUpdateShelf, books, onUpdateBooksState } = this.props
return(
<div className="search-books">
<div className="search-books-bar">
<a className="close-search" >Close</a>
<div className="search-books-input-wrapper">
<input
type="text"
placeholder="Search by title, author or subject"
value={query}
onChange={(event) => this.updateQuery(event.target.value)}
/>
</div>
</div>
<div className="search-books-results">
<ol className="books-grid">
<li>
{ results ? (
results.map((book) => (
<Book
key={book.id}
book={book}
updateShelf={onUpdateShelf} // onUpdateShelf used here
/>
))
) : (
<h4>No results for, "{query}"</h4>
)}
</li>
</ol>
</div>
</div>
)
}
}
Compiler Warning: 编译器警告:
./src/SearchBooks.js Line 45: 'books' is assigned a value but never used no-unused-vars Line 45: 'onUpdateBooksState' is assigned a value but never used no-unused-vars
Chrome warning: Chrome警告:
webpackHotDevClient.js:138 ./src/SearchBooks.js
Line 46: 'books' is assigned a value but never used no-unused-vars
Line 46: 'onUpdateBooksState' is assigned a value but never used no-unused-vars
Its an EsLint warning to prevent you from declaring un-used variables. 它是一个EsLint警告,以防止您声明未使用的变量。
In this line you are declaring (NOT USING) the variables 在这一行中,您声明(不使用)变量
const { onUpdateShelf, books, onUpdateBooksState } = this.props
The variables books
and onUpdateBooksState
are not used anywhere in your code. 变量
books
和onUpdateBooksState
不在代码中的任何地方使用。
Just get rid of them and change the line to 摆脱它们,将线更改为
const { onUpdateShelf } = this.props
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.