[英]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} />)}</>)
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.