Rui*_*zhi 2 javascript reactjs arrow-functions redux
我已经为此挣扎了很长时间。我们都知道箭头函数简化了语法,如下所示:
A. 箭头函数:
onClick={() => toggleTodo(todo.id)}
Run Code Online (Sandbox Code Playgroud)
B. 展开箭头函数:
onClick={() => {
// we can see there is a return keyword here. can I just remove the return keyword ?
return toggleTodo(todo.id);
^^^^^^
}}
Run Code Online (Sandbox Code Playgroud)
在官方 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)
Run Code Online (Sandbox Code Playgroud)
问题是:在onSubmit,为什么我们不需要return在函数体里面放一个?谢谢你。
return如果返回值将在某处使用,您只需要一些东西。在 的情况下onSubmit,不需要return任何东西。该(箭头)函数只是在提交表单时运行一些代码。
在您的问题中的B点,是的,return 如果不需要对toggleTodo(todo.id).
| 归档时间: |
|
| 查看次数: |
476 次 |
| 最近记录: |