- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 React 教程中看到了一段代码片段:
const App = ({title}) => (
<div className="header">{title}</div>
);
乍一看,我以为它给 App
分配了一个箭头功能持续的。然后我注意到它不使用花括号而是括号。
我理解箭头函数应该是 (...) => {...}
,但这里使用 (...) => (...)
那么,它到底是不是箭头函数呢?如果是的话,为什么还有另一种形式?我如何决定何时使用哪种表格?如果不是的话,这个函数类型在js中调用的是什么?
最佳答案
是的,它也是一个箭头函数。唯一的区别是,如果不使用大括号,它会强制返回:
const App = () => { return true; } // with braces you've to use the return statement
const App = () => true; // without braces it forces the return statement automatically
MDN arrow function expression documentation对此有如下说法:
Function body
Arrow functions can have either a "concise body" or the usual "blockbody".
In a concise body, only an expression is specified, which becomes theimplicit return value. In a block body, you must use an explicit
return
statement.var func = x => x * x;
// concise body syntax, implied "return"
var func = (x, y) => { return x + y; };
// with block body, explicit "return" needed
此外,关于括号:本例中的箭头函数返回一个 JSX 表达式,它被视为单个对象。括号主要用于多行 JSX 代码。在这里查看更多信息:https://reactjs.org/docs/introducing-jsx.html还有关于堆栈溢出的其他类似问题。
关于javascript - 箭头函数语法用括号而不是花括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60596873/
我用 redis 后端运行 celery。 我想在 centos 6.2 上运行 celery 花作为守护进程。 我知道花是 Tornado 应用程序,所以我应该使用一个进程来运行 Tornado 应
我在一台服务器上有几个项目,它们使用具有不同 BROKER_URL 的 celery 包。Flower 允许一个 BORKER_URL 作为命令选项: celery flower --broker=a
我正在使用芹菜和花卉。当我访问Flower中的“任务”标签时,我可以看到我的任务正在注册,甚至可以在“状态”列中看到“成功”标签以及所有内容。 但是,在“监视器”选项卡上,所有图形(“成功任务”,“失
我正在尝试创建一个有 5 个叶子的 css 花 - 没有任何有希望的尝试。我正在尝试创建什么:有 5 片叶子的 CSS 花,每片叶子可以有三种不同的状态(小、中、大)。我找到了 http://ross
如果我有一个变体,像这样: using my_variant = boost::variant; 有没有一种简单的方法可以将变体可以包含的类型提取到 Boost.Hana 元组中,以便满足以下条件:
调用 Flower API's /api/task/apply/* 只是挂起而没有返回结果,即使任务已根据 Web UI 处理并成功。 重现错误: # hangs even though task s
我有一个模板函数,默认情况下没有定义,但它由某些类型特化: template auto foo(bar &, const T &) -> void; template <> auto foo(bar
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我是一名优秀的程序员,十分优秀!