简体   繁体   English

我可以在 JavaScript 中省略箭头函数中的返回值吗?

[英]Can I omit Return value in arrow function in JavaScript?

I've been struggled from this for a long time.我已经为此挣扎了很长时间。 we all know that arrow function simplifies the syntax, like this:我们都知道箭头函数简化了语法,如下所示:

A. Arrow function: A. 箭头函数:

        onClick={() => toggleTodo(todo.id)}

B. Expand the Arrow function: B. 展开箭头函数:

        onClick={() => {
           // we can see there is a return keyword here. can I just remove the return keyword ? 
           return toggleTodo(todo.id); 
           ^^^^^^
        }}

In the official redux tutorial, the file AddTodo.js :在官方 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)

The question is: in the onSubmit , why we don't need to put a return inside the function body ?问题是:在onSubmit ,为什么我们不需要在函数体内放置return Thank you.谢谢你。

You only need to return something if that returned value will be used somewhere.如果返回的值将在某处使用,您只需要return一些东西。 In the case of onSubmit , there is no need to return anything.onSubmit的情况下,不需要return任何东西。 That (arrow) function is simply running some code when the form is submitted.该(箭头)函数只是在提交表单时运行一些代码。

In point B in your question, yes, you can remove return if nothing needs to be done with the returned result of toggleTodo(todo.id) .在您的问题中的B点,是的,如果不需要对toggleTodo(todo.id)的返回结果执行任何操作,您可以删除return

They are both arrow functions它们都是箭头函数

onClick={() => toggleTodo(todo.id)}

Short version精简版

 onClick={() => { toggleTodo(todo.id); }}

Long version长版

 onClick={() => { toggleTodo(todo.id); toggleTodo2(todo.id); toggleTodo3(todo.id); }}

Long version can have multiple calls, return not required长版本可以有多个调用,不需要return

You need a return value when you use a synchronized function and need its result.当您使用同步函数并需要其结果时,您需要一个返回值。

Whenever you use an asynchronous function, it means that the calling function already continued with itself and moving to the next phase will be via a callback function.每当您使用异步函数时,这意味着调用函数已经继续使用它自己,并且将通过回调函数移动到下一阶段。

In your case the window object is the caller of the onClick , so there's no reason to return a value to it, it will have nothing to do with it.在您的情况下, window对象是onClick的调用者,因此没有理由向其返回值,它与它无关。

You do want to trigger react's rendering mechanism so you use the dispatch callback/trigger.您确实想触发 react 的渲染机制,因此您可以使用调度回调/触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM