Reactjs &引用;“没有未使用的表达”;使用映射函数进行反应时出错
我创建了一个“App”类,并在子“Todos”组件中作为道具传递了一个状态 看看我的代码:Reactjs &引用;“没有未使用的表达”;使用映射函数进行反应时出错,reactjs,components,es6-map,Reactjs,Components,Es6 Map,我创建了一个“App”类,并在子“Todos”组件中作为道具传递了一个状态 看看我的代码: class App extends Component { state = { todos:[ { id:1, title:'task1', complete:false, },{ id:2, title:'task2', complete:false, },{ id:3, t
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>
);
}
}
现在,在map函数中的Todos组件中,它不允许我使用大括号,但如果我用圆括号替换它,这很好,为什么
请帮帮我。很抱歉这个结构不好的问题。如果您使用大括号,这意味着您正在编写一个函数,最后应该返回一些
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);
除非使用显式的
返回值,否则它不会返回带花括号的任何内容。警告是什么?它是否告诉您映射函数返回未定义的
?
return this.props.todos.map((list)=>{ return list.title});
return this.props.todos.map((list)=> list.title);