Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react export function vs export const: FC

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 /> }
like image 408
wynx Avatar asked Mar 20 '20 03:03

wynx


People also ask

What is the difference between export and export default in React?

Exports without a default tag are Named exports. Exports with the default tag are Default exports. Using one over the other can have effects on your code readability, file structure, and component organization. Named and Default exports are not React-centric ideas.

Can we export Const in React?

In order to be able to import a constant from a different file, it has to be exported using a named or default export. The example above uses a named export and a named import.

What is export function in React?

Use named exports to export multiple functions in React, e.g. export function A() {} and export function B() {} . The exported functions can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.

Can I export function from functional component?

Exporting a function from a functional component in order to be used by a parent component is possible. All you have to do is to make good use of refs.


1 Answers

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
like image 84
Daniel Bank Avatar answered Sep 19 '22 02:09

Daniel Bank