Why do we use curly brace while importing some library?
Why do we use curly brace while importing some library?
In React Native, curly braces are used in import statements to import named exports from a module. This is a common practice in JavaScript ES6 and is essential for distinguishing between default exports and named exports.
Default Exports:
// module.js
const myDefaultExport = 'default';
export default myDefaultExport;
// main.js
import myDefaultExport from './module';
Named Exports:
// module.js
export const namedExport1 = 'export1';
export const namedExport2 = 'export2';
// main.js
import { namedExport1, namedExport2 } from './module';
Curly braces are used to specify exactly which named exports you want to import from a module. This is necessary because named exports are not the default export and need to be explicitly identified.
In React Native, you often see imports like this:
import React, { useState } from 'react';
React
is the default export from the 'react' module.useState
is a named export from the same module.junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào