简体   繁体   English

如何解决 NodeJS 中的这个 no-unused-vars 错误?

[英]how to solve this no-unused-vars error in NodeJS?

I am creating a todo app using MERN stack.I am new to MERN stack technology and I kindly neeed your help solving this error.I have provided the code for my app.js file and todo.js file.I can't clearly find the solution of this error anywhere on the internet.我正在使用 MERN 堆栈创建一个 todo 应用程序。我是 MERN 堆栈技术的新手,我需要您的帮助来解决这个错误。我已经提供了我的 app.js 文件和 todo.js 文件的代码。我无法清楚地找到互联网上任何地方的此错误的解决方案。 I am getting this error while runnng the node js app using npm start command.使用 npm 启动命令运行节点 js 应用程序时出现此错误。

Compiled with warnings.

src\App.js
  Line 4:8:  'Todo' is defined but never used  no-unused-vars

Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.

App.js应用程序.js

import React from 'react';
import logo from './logo.svg';
import './App.css';
import Todo from './components/Todo.js';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

Todo.js Todo.js

import React, { Component } from 'react'
import axios from 'axios';

 // eslint-disable-next-line

export class Todo extends Component {
    constructor(props) {
        super(props)
    
        this.state = {
             todos : [],
             item : ""
        }
    }

    changeHandler = (event) => {
        this.setState({item: event.target.value})
    }

    clickHandler = (event) => {
        event.preventDefault()
        console.log(this.state.item)
        axios({
            method: 'post',
            url: 'http://localhost:3000/',
            data: {
              todo: this.state.item,
            }
          });
        this.setState({item:''})
    }

    componentDidMount() {
        axios.get('http://localhost:3000/').then((response) => {
            console.log(response.data.data)
            let data = [];
            console.log(response.data.data.length)
            for(var i =0; i < response.data.data.length; i++){
                data.push(response.data.data[i].todo)
            }
            this.setState({todos: data})
        });
    }
    componentDidUpdate() {
        axios.get('http://localhost:3000/').then((response) => {
            console.log(response.data.data)
            let data = [];
            console.log(response.data.data.length)
            for(var i =0; i < response.data.data.length; i++){
                data.push(response.data.data[i].todo)
            }
            this.setState({todos: data})
        });
    }
  
    render() {
        
        return (
            <div>
                <input type="text" onChange={this.changeHandler}/>
                <button type="submit" onClick={this.clickHandler}>add</button>
                <div>
                    <ul>{this.state.todos.map((todo, index) => <li key={index}>{todo}</li>)}</ul>
                </div>
            </div>
        )
    }
}

export default Todo


That warning you are getting because even though you are importing Todo file in your App.js file but you aren't using it anywhere.Either try using it in App.js or remove the import(in case you don't need it).That should fix the warning.您收到该警告是因为即使您在 App.js 文件中导入 Todo 文件但您没有在任何地方使用它。尝试在 App.js 中使用它或删除导入(以防您不需要它) .那应该修复警告。 Or add // eslint-disable-next-line just before the import Todo.. statement in App.js或者在 App.js 中的 import Todo.. 语句之前添加// eslint-disable-next-line

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM