3therk1ll
3therk1ll

Reputation: 2421

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.

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:

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

Upvotes: 0

Views: 374

Answers (1)

supra28
supra28

Reputation: 1636

Its an EsLint warning to prevent you from declaring un-used variables.

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.

Just get rid of them and change the line to

const { onUpdateShelf } = this.props

Upvotes: 1

Related Questions