简体   繁体   English

为使用的var编译警告no-unused-vars

[英]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. 变量booksonUpdateBooksState不在代码中的任何地方使用。

Just get rid of them and change the line to 摆脱它们,将线更改为

const { onUpdateShelf } = this.props

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 组件已定义但从未使用 reactjs 中的 no-unused-vars 错误警告 - component is defined but never used no-unused-vars error warning in reactjs 定义了“ stringValues”,但从未使用过no-unused-vars - “stringValues” is defined but never used no-unused-vars 定义了“标头”,但从未使用过未使用的变量 - 'Header' is defined but never used no-unused-vars &#39;Home&#39; 已定义但从未使用 no-unused-vars&quot;.* - 'Home' is defined but never used no-unused-vars".* &#39;UserInput&#39; 已定义但从未使用过 no-unused-vars &#39;UserOutput&#39; 已定义但从未使用过 no-unused-vars - 'UserInput' is defined but never used no-unused-vars 'UserOutput' is defined but never used no-unused-vars Vue.js 编译错误:no-unused-vars &amp; no-undef - Vue.js Compile Error: no-unused-vars & no-undef &#39;inStock&#39; 被赋值但从未使用过 no-unused-vars - 'inStock' is assigned a value but never used no-unused-vars 11:5 错误“路由器”被分配了一个值,但从未使用过 no-unused-vars - 11:5 error 'router' is assigned a value but never used no-unused-vars ESLint 抱怨从未使用过本地 state 变量 no-unused-vars - ESLint complains that a local state variable is never used no-unused-vars ESLint no-unused-vars和no-undef错误但使用了变量 - ESLint no-unused-vars and no-undef error but variable is used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM