Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need default export in React?

I know there are two kinds of exports, one is named default and other is the default export.

// utils.js
const addOne = (a) => a+1;
export default addOne;
export const subOne = (a) => a- 1;

//app.js
import plusOne, {subOne} from './utils.js'

I am so confused about why we need a default export, it just can use only once. The only reason is the named export provide the name locally, and the default export can rename. However, if we have namely demand, maybe we need to use it repeatedly.

like image 655
Cvanzy Avatar asked Sep 06 '26 08:09

Cvanzy


1 Answers

The default import isn't required. You can use named exports all you want. In your case, when you're exporting a bunch of utility functions, it would likely be better to use named exports. However, if you need to export just one function, usually a class, a default export is ideal.

like image 103
Smarticles101 Avatar answered Sep 07 '26 23:09

Smarticles101



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!