我可以忽略JavaScript中箭头功能的返回值吗?

问题描述 投票:0回答:2

我已经为此苦了很长时间了。我们都知道箭头功能简化了语法,如下所示:

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?谢谢。

javascript reactjs redux arrow-functions
2个回答
3
投票

如果返回的值将在某处使用,则只需return。在onSubmit的情况下,无需执行任何操作。提交表单时,该(箭头)功能只是在运行一些代码。

在问题中的[[B]点],是的,您可以删除return 如果返回的return结果无需执行任何操作。


0
投票
它们都是箭头功能

0
投票
使用同步函数并需要其结果时需要返回值。
© www.soinside.com 2019 - 2024. All rights reserved.