简体   繁体   English

React 中的待办事项列表应用程序错误:no-unused-expressions

[英]Todo List App in React Error: no-unused-expressions

So I am trying to make a To-Do-List App in ReactJS but I got an error:所以我试图在 ReactJS 中制作一个待办事项列表应用程序,但出现错误:

/src/todoList.js Line 40:21: Expected an assignment or function call and instead saw an expression no-unused-expressions. /src/todoList.js 第 40:21 行:需要一个赋值或 function 调用,而是看到一个表达式 no-unused-expressions。

I am storing all the tasks in the state of Todolist and using a .map() which returns another component called TodoItem.我将所有任务存储在 Todolist 的 state 中,并使用.map()返回另一个名为 TodoItem 的组件。 I am pretty new to the react environment and have been not able to figure out the error myself.我对反应环境还很陌生,自己也无法找出错误。

import React from 'react';
import TodoItem from './components/todoItem';

class TodoList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            todos: [
                {
                    id: 1,
                    title: 'Take out the trash',
                    completed: false
                },
                {
                    id: 2,
                    title: 'Clean the room',
                    completed: false
                },
                {
                    id: 3,
                    title: 'Wash the dishes',
                    completed: false
                },
            ]
        }
    }

    render() {
       const todos = this.state.todos;
      
       
            return (
               
                   todos.map((todo) => {
                    //console.log(todo);
                    <TodoItem todo = {todo} />
                    })
               
                
            )
    
       
    }
}

export default TodoList;

The TodoItem Component. TodoItem组件。 It is a separate component for each item of the Todo list.它是 Todo 列表中每个项目的单独组件。

import React from 'react';

class TodoItem extends React.Component {
    
    render() {
        console.log(this.props);
        return (
            <h3>{this.props.todo.title}</h3>
        )
    }
}

export default TodoItem;

The problem is here:问题在这里:

return (
               
                   todos.map((todo) => {
                    //console.log(todo);
                    <TodoItem todo = {todo} />
                    })

Should be:应该:

return (<>{todos.map((todo) => <TodoItem todo = {todo} />)}</>)
  • you do well just a tiny mistake happened in the map method you didn't return any thing so it gave you a warning that there is nothing to render so the solution is this:你做得很好,只是在map方法中发生了一个小错误,你没有返回任何东西,所以它警告你没有要渲染的东西,所以解决方案是这样的:
import React from 'react';
import TodoItem from './components/todoItem';

class TodoList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            todos: [
                {
                    id: 1,
                    title: 'Take out the trash',
                    completed: false
                },
                {
                    id: 2,
                    title: 'Clean the room',
                    completed: false
                },
                {
                    id: 3,
                    title: 'Wash the dishes',
                    completed: false
                },
            ]
        }
    }

    render() {
       const todos = this.state.todos;
      
       
            return (
               
                   todos.map((todo) => {
                    //console.log(todo);
                    return <TodoItem todo = {todo} />
                    })
               
                
            )
    
       
    }
}

export default TodoList;

You should use () instead of {} in the map function like this:您应该在 map function 中使用 () 而不是 {},如下所示:

 return (               
      todos.map((todo) => (            
          <TodoItem todo = {todo} />
      ))             
            
  )

Or add return statement if you want to use curly brackets like this:或者如果您想使用这样的大括号,请添加 return 语句:

return (               
      todos.map((todo) => {            
          return <TodoItem todo = {todo} />
      })             
            
  )

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

相关问题 没有未使用的表达式 React - no-unused-expressions React 带有条件的 React 中的 no-unused-expressions - no-unused-expressions in React with conditionals No-unused-expressions 错误在 React 中的脚本导入时不断出现 - No-unused-expressions error keeps recurring at script import in React Eslint(没有未使用的表达式) - Eslint (no-unused-expressions) 三元上没有未使用的表达式 - no-unused-expressions on ternary 得到预期的赋值或函数调用的错误,而是在反应中看到了一个表达式 no-unused-expressions - getting an error of Expected an assignment or function call and instead saw an expression no-unused-expressions in react 期望一个赋值或函数调用,而是在React中看到一个表达式no-unused-expressions错误 - Expected an assignment or function call and instead saw an expression no-unused-expressions error in React ESLint-ReactJS中没有未使用的表达式 - ESLint - no-unused-expressions in ReactJS 如何摆脱`no-unused-expressions` eslint 错误,用于`&amp;&amp;` 和`?:` 用法? - How to get rid of `no-unused-expressions` eslint error, for `&&` and `?:` usage? 获得预期的赋值或函数调用,而是在尝试在 React 中呈现组件时看到表达式 no-unused-expressions 错误 - Getting Expected an assignment or function call and instead saw an expression no-unused-expressions error when trying to render a component in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM