Câu hỏi phỏng vấn React Native
Câu hỏi

Why do we use curly brace while importing some library?

Câu trả lời

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 vs. Named Exports

  1. Default Exports:

    • A module can have only one default export.
    • When importing a default export, you do not use curly braces.
    • Example:
      // module.js
      const myDefaultExport = 'default';
      export default myDefaultExport;
      
      // main.js
      import myDefaultExport from './module';
  2. Named Exports:

    • A module can have multiple named exports.
    • When importing named exports, you must use curly braces.
    • Example:
      // module.js
      export const namedExport1 = 'export1';
      export const namedExport2 = 'export2';
      
      // main.js
      import { namedExport1, namedExport2 } from './module';

Why Use Curly Braces?

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.

Example in React Native

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.

Summary

  • Curly braces are used to import named exports.
  • No curly braces are used to import default exports.
  • This distinction helps in modularizing code and ensuring that only the necessary parts of a module are imported, which can improve code readability and...
junior

junior

Gợi ý câu hỏi phỏng vấn

senior

How would you implement animations on events?

middle

What is "Fast Refresh"?

expert

What is InteractionManager and how is it used?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào