最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react export function vs export const: FC - Stack Overflow

programmeradmin1浏览0评论

So I am just wondering what is the difference or reasons to use one over the other...

export function Name() { return <div /> }

vs

export const Name = () => { return <div /> }

So I am just wondering what is the difference or reasons to use one over the other...

export function Name() { return <div /> }

vs

export const Name = () => { return <div /> }
Share Improve this question edited Mar 20, 2020 at 3:57 keikai 15.1k9 gold badges54 silver badges71 bronze badges asked Mar 20, 2020 at 3:50 wynxwynx 3231 gold badge2 silver badges14 bronze badges 1
  • 1 it's for export individual feature for check url – Savaj Patel Commented Mar 20, 2020 at 6:51
Add a comment  | 

1 Answer 1

Reset to default 17

Pragmatically (i.e. when building functional components in React), there is no difference between using a named function vs. exporting an arrow function as the value of a named export.

In both cases you are exporting a function that is (hopefully) not using the this keyword. Thus you don't have to worry about one of the most important differences between a function and an arrow function, which is whether you need this to be lexically bound vs dynamically bound.

Also as you are assigning a variable to the arrow function, you don't have to worry about reduced traceability in debugging the arrow function. JavaScript is able to infer the function names.

As you probably know, it would matter if you were exporting the component as a default export because then you can't give a name to a default export. You would need to use two lines:

const Name = () => { return <div /> }
export default Name
发布评论

评论列表(0)

  1. 暂无评论