[英]multiple return in rendering a list in jsx
Is there any way to refactor below code to make it more clean? 有什么方法可以重构下面的代码以使其更干净? Especially the multiple return in
renderTodos
method. 特别是
renderTodos
方法中的多次返回。 I also have a confusion, am I creating the renderTodos
method correctly? 我也有一个困惑,我是否正确创建了
renderTodos
方法? Should I place it in the render function? 我应该将其放置在render函数中吗? or doing
this.renderTodos()
is ok? 或这样做
this.renderTodos()
可以吗?
export default class TodoList extends Component {
renderTodos = () => {
const { todos } = this.props
return todos.map(todo => {
return (
<Todo key={todo.id} {...todo} />
)
})
}
render() {
return(
<div>
{this.renderTodos()}
</div>
)
}
}
Since you just want to return the element without any condition or calculation, so you can avoid {}
and return
with map. 由于您只想返回没有任何条件或计算的元素,因此可以避免
{}
并return
map。
Like this: 像这样:
renderTodos = () => {
const { todos } = this.props;
return todos.map(todo => <Todo key={todo.id} {...todo} /> )
}
With this way return inside renderTodos
is required to return the result from that method. 通过这种方法,需要在
renderTodos
内部返回,以从该方法返回结果。
Another way is put the map part directly inside render method, like this: 另一种方法是将地图部件直接放在render方法内,如下所示:
render() {
return(
<div>
{
this.props.todos.map(todo => <Todo key={todo.id} {...todo} /> )
}
</div>
)
}
Or better to write it as Stateless Functional Component : 或者最好将其编写为无状态功能组件 :
const TodoList = (props) => (
<div>
{
props.todos.map(todo => <Todo key={todo.id} {...todo} /> )
}
</div>
)
Check this article: Presentational and Container Components 检查本文: 演示和容器组件
If you are not using renderTodos
as a callback to be passed as a prop
, you can render it directly in the final render()
. 如果您没有使用
renderTodos
作为要作为prop
传递的回调,则可以直接在最终的render()
渲染它。
render() {
return(
<div>
{props.todos.map(todo => <Todo key={todo.id} {...todo} />}
</div>
)
}
I'm also using implicit return here. 我也在这里使用隐式返回。
Also, since your component doesn't maintain any state
, and just accepts props
and renders JSX, you can turn it into a stateless functional component - 另外,由于您的组件不维护任何
state
,仅接受props
并呈现JSX,因此您可以将其变成无状态的功能组件-
const TodoList = props => {
return(
<div>
{props.todos.map(todo => <Todo key={todo.id} {...todo} />}
</div>
)
}
This is a stateless component so you can just use a plain function instead of a class. 这是一个无状态组件,因此您可以只使用普通函数而不是类。 Additionally, you don't actually have a multiple return in renderTodos since one of the return statements is actually another function entirely (though you can make it a one liner pretty easily removing the necessity for the return statement).
另外,在renderTodos中实际上没有多重返回,因为其中一个return语句实际上完全是另一个函数(尽管您可以使其成为一个线性函数,从而很容易地消除了return语句的必要性)。
Note that the spaceship operator "=>" signifies that the parameter of .map is an ES6 arrow function . 请注意,太空飞船运算符“ =>”表示.map的参数是ES6箭头函数 。
function Todo(props) {
return props.todos.map(todo => <Todo key={todo.id} {...todo} />);
}
Since you don't have state, you can make your entire component functional . 由于您没有状态,因此可以使整个组件正常运行 。
function TodoList(props) {
return <div>{props.todos.map(todo => <Todo key={todo.id} {...todo} />)</div>;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.