reactjs - "no-unused-expressions" error in react with map function
问题描述
I made an 'App' class and passed a state as props in a child 'Todos' component.
Have a look at my code:
class App extends Component {
state = {
todos:[
{
id:1,
title:'task1',
complete:false,
},{
id:2,
title:'task2',
complete:false,
},{
id:3,
title:'task3',
complete:false,
}
]
}
render() {
return (
<div className="App">
<Todos todos={this.state.todos}/>
</div>
);
}
}
My Todos Component:
class Todos extends Component {
render() {
return this.props.todos.map((list)=>{list.title});
}
}
Now inside my Todos component in map function,it's not allowing me to use curly braces but if I replace it by round bracket,it's fine,Why?
Please help me.Sorry for badly structured question.
解决方案
如果你使用花括号,这意味着你正在编写一个函数,你应该jsx
在最后返回一些,但是你的代码没有返回任何jsx
. 所以有效的代码是
return this.props.todos.map((list)=>{ return list.title});
并且带有圆括号的代码可以工作,因为它是写返回的简写。所以基本上用圆括号,你的代码还是这样
return this.props.todos.map((list)=>{ return list.title});
一些有效的方法:
return this.props.todos.map((list)=>{ return list.title});
return this.props.todos.map((list)=> list.title);
推荐阅读
- javascript - 如何在 React App 中保护 Auth0 凭据?
- c - 带有 CLion 的 C 中的多线程(Windows)
- reactjs - 用于反应的 Eslint 配置
- python - 在python中从tsv构造矩阵
- flutter-web - 如何在 Flutter web 中截取小部件的屏幕截图
- google-app-engine - Google App Engine 自定义域不会从 DNS 中释放我的域名
- java - 如何使用 REST API 调用将非常大的 postgres 数据库表同步到 Web 软件?
- mongodb - Meteor 和 GroundDB:按 id 查找不返回任何条目
- javascript - 如何修复 Vue 警告:组件渲染函数中可能存在无限更新循环
- python - subprocess.check_output() : 设置 parm executable='/bin/bash' 时找不到 bash 命令