In react hooks, I found that the import statement uses: import { useState } from 'react'; Why does it use the {} around the useState?
Asked
Active
Viewed 1,129 times
2
-
This is not React-specific, that's just how you import a non-default export. – jonrsharpe Jul 09 '20 at 17:25
-
1To avoid confusions like this I do recommend you to first learn some javascript basics before digging into libraries and frameworks. – Yury Tarabanko Jul 09 '20 at 17:33
-
I don't think object destructuring is basic JavaScript. – user31782 Jun 06 '23 at 14:40
1 Answers
0
You use curly braces to import named modules, that is if it was exported using code like this:
const myModule = "example exported value";
export { myModule };
You can import module without curly braces if it was exported as a default module, that is:
const myModule = "example exported value";
export default myModule;
So to answer your question. Default React object is exported as a default and other modules are exported as named modules.
Here's an example on how you can do it yourself: https://codesandbox.io/s/compassionate-ride-g893e?file=/src/index.js
You can read more about importing and exporting modules in JavaScript on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

Jan Jaworski
- 139
- 3
- 12