简体   繁体   English

在React / Redux中的箭头功能内使用方括号有什么意义

[英]What is the significance of using brackets inside an arrow function in React/Redux

Given the following react-redux code: 给定以下react-redux代码:

const TextListContainer = ({ items, actions }) => (
    <TextList items={items} actions={actions} />
)

Why are normal brackets used here instead of curly brackets? 为什么在这里使用普通括号而不是大括号?


To further illustrate my question: 为了进一步说明我的问题:

NORMAL FUNCTION: 正常功能:

const someFn = something => {
    //...
}

BRACE STYLE FUNCTION: 大括号样式功能:

const someFn = something => (
    //...
)

This style of code is copied from here: https://github.com/reactjs/redux/blob/master/examples/todomvc/src/containers/App.js 这种样式的代码从这里复制: https : //github.com/reactjs/redux/blob/master/examples/todomvc/src/containers/App.js

() => something , where something doesn't start with { , returns something . () => something ,其中something不与启动{返回something

With () => { , { is interpreted as the start of a function body, so you have to explicitly return something. 使用() => {{被解释为函数体的开始,因此您必须显式return某些内容。 To get around this, eg if you wanted to return an object, you can use ( : 为了解决这个问题,例如,如果您想返回一个对象,可以使用(

() => ({ some object })

Using it in other situations is a question of consistency. 在其他情况下使用它是一致性的问题。

Basically {} is used when there is function body and you need to return a particular value based on the computations in the function. 基本上, {}用于有函数体的情况,您需要根据函数中的计算返回特定值。 A simple example of a function to add 1 if value is more than 10 else subtract 1 . 一个简单的函数示例,如果值大于10,则加1,否则减去1。

(value) => {
  if(value > 10){
    return value + 1; 
  }
  return value - 1;
}

On the other hand if there is really a simple function like returning a boolean value.you could do something like this: 另一方面,如果确实有一个简单的函数,例如返回布尔值,则可以执行以下操作:

(num) => (num > 100)

returns a boolean. 返回一个布尔值。 Simple and clean. 简单干净。

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

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