2

In react hooks, I found that the import statement uses: import { useState } from 'react'; Why does it use the {} around the useState?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
adi
  • 143
  • 11

1 Answers1

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