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 /> }
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With