IMPORT - EXPORT

 IN REACT,WE HAVE WEBPACK THAT ENABLES IMPORT AND EXPORT.

IMPORT

import component_name from file_name

import("./App.css");
import Title from "./TItle.jsx";

function App() {
 
  return (
      <Title />
  );
}

export default App;

EXPORT

WE CAN EPORT FILES IN 2 WAYS,

1. DEFAULT EXPORT

2. NAMED EXPORT


DEFAULT EXPORT

WE USE DEFAULT EXPORT,WHEN WE HAVE A SINGLE VALUE THAT HAS TO BE EXPORTED AND IT MIGHT HAVE A CUSTOM NAME IN ANOTHER FILE.

export default component_name;

THE App COMPONENT FROM App.jsx IS EXPORTED AND IT IS IMPORTED IN main.jsx

export default App;


NAMED EXPORT

WHEN WE HAVE TO EXPORT MULTIPLE COMPONENTS.

function Title() {
  return <h2>This is the TITLE!!!</h2>;
}

function Sum(a, b) {
  return a + b;
}
export { Title, sum };


IT IS IMPORTED AS,

import {Title,Sum} from "./TItle.jsx";

 IN BOTH CASES, THE CODE WORKS PERFECTLY.

IN SUMMARY, NAMED EXPORTS ARE USEFUL WHEN YOU WANT TO EXPORT MULTIPLE VALUES AND IMPORT THEM WITH THEIR SPECIFIC NAMES.

WHILE DEFAULT EXPORTS ARE USED FOR EXPORTING A SINGLE VALUE AND GIVING IT A CUSTOM NAME WHEN IMPORTING.

THE CHOICE BETWEEN THE TWO DEPENDS ON THE STRUCTURE AND REQUIREMENTS OF YOUR CODEBASE.





Comments

Popular posts from this blog

DEPENDENCIES IN useEffect()

ACTIVITY1

CONDITIONALS