我已经为此苦了很长时间了。我们都知道箭头功能简化了语法,如下所示:
A。箭头功能:
onClick={() => toggleTodo(todo.id)}
B。展开箭头功能:
onClick={() => {
// we can see there is a return keyword here. can I just remove the return keyword ?
return toggleTodo(todo.id);
^^^^^^
}}
在官方的redux教程中,文件AddTodo.js
:
import React from 'react'
import { connect } from 'react-redux'
import { addTodo } from '../actions'
const AddTodo = ({ dispatch }) => {
let input
return (
<div>
<form
onSubmit={e => { // focus on this line
e.preventDefault()
dispatch(addTodo(input.value)). // focus on this line, if i put return here, the code below won't be executed. if not, how to explain the syntax in the beginning?
input.value = ''
}}
>
<input ref={node => (input = node)} />
<button type="submit">Add Todo</button>
</form>
</div>
)
}
export default connect()(AddTodo)
问题是:在onSubmit
中,为什么我们不需要在函数体内放入return
?谢谢。
如果返回的值将在某处使用,则只需return
。在onSubmit
的情况下,无需执行任何操作。提交表单时,该(箭头)功能只是在运行一些代码。
在问题中的[[B]点],是的,您可以删除return
如果返回的return
结果无需执行任何操作。