其实它们之间没有区别,我在CodeSandBox上做了一个little project,并做了两个简单的组件,其中一个是Arrow组件,使用箭头函数: p>
import React from 'react';
const MyArrowComponent = () => (
<main>
<h2>Arrow</h2>
</main>
);
export default MyArrowComponent;
另一个是Declaration组件,使用函数声明:
import React from "react";
function MyFunctionComponent() {
return (
<main>
<h2>Declaration</h2>
</main>
);
}
export default MyFunctionComponent;
然后我运行yarn build 命令并得到如下包:
(window.webpackJsonp = window.webpackJsonp || []).push([[0], {
14: function (e, n, t) {
"use strict";
t.r(n);
var a = t(0), r = t.n(a), l = t(2),
c = t.n(l), u = t(3), i = t(4), o = t(6), m = t(5), E = t(7);
var p = function () {
return r.a.createElement("main", null, r.a.createElement("h2", null, "Declaration"))
}, s = function () {
return r.a.createElement("main", null, r.a.createElement("h2", null, "Arrow"))
}, d = function (e) {
function n() {
return (
Object(u.a)(this, n),
Object(o.a)(this, Object(m.a)(n).apply(this, arguments))
}
return Object(E.a)(n, e), Object(i.a)(n, [{
key: "render", value: function () {
return r.a.createElement(
'div',
null,
r.a.createElement('div', null, 'Hi'),
r.a.createElement(p, null),
r.a.createElement(s, null)
);
}
}]), n
}(r.a.Component);
c.a.render(r.a.createElement(d, null), document.getElementById("root"))
}, 8: function (e, n, t) {
e.exports = t(14)
}
}, [[8, 1, 2]]]);
注意Arrow和Declaration组件的定义:
var p = function () {
return r.a.createElement("main", null, r.a.createElement("h2", null, "Declaration"))
}, s = function () {
return r.a.createElement("main", null, r.a.createElement("h2", null, "Arrow"))
}
两者的定义方式相同,所以绝对没有区别,完全基于开发者对代码可读性和代码整洁的态度,我们团队基于 ESLint 5.x 我们选择 箭头函数定义功能组件。