Code Monkey home page Code Monkey logo

flw2-u2l2-23-24-student-exercises-part-2's Introduction

Lesson 2.2: Importing & Exporting Components

In React, a component can be defined as a reusable piece of UI. Components can be imported and exported between different files, making your codebase modular and easier to maintain.

Exporting Components

Before a component can be used in other parts of your application, it must be exported from the file where it's defined. There are two types of exports: named exports and default exports.

Named Exports

With named exports, you can export multiple items from a single file. Each exported item must have a unique name.

// MyComponent.js
import React from 'react';

export const MyComponent = () => {
  return <h1>Hello, World!</h1>;
};

Default Exports

A file can have a single default export. When importing a default export, you can name it whatever you like.

// MyDefaultComponent.js
import React from 'react';

const MyDefaultComponent = () => {
  return <h1>Hello, World!</h1>;
};

export default MyDefaultComponent;

Importing Components

Once a component is exported, it can be imported into other files.

Named Imports

For named exports, use curly braces { } to import the component with its exported name.

// App.js
import React from 'react';
import { MyComponent } from './MyComponent';

const App = () => {
  return <MyComponent />;
};

export default App;

Default Imports

For default exports, you can import the component using any name you choose without curly braces.

// App.js
import React from 'react';
import AnyName from './MyDefaultComponent';

const App = () => {
  return <AnyName />;
};

export default App;

Example

Below is an example that puts everything together.

File Structure

src/
|-- components/
|    |-- Header.js
|    |-- Footer.js
|
|-- App.js

Header.js

// Named export
import React from 'react';

export const Header = () => {
  return <header>Header Section</header>;
};

Footer.js

// Default export
import React from 'react';

const Footer = () => {
  return <footer>Footer Section</footer>;
};

export default Footer;

App.js

import React from 'react';
import { Header } from './components/Header'; // Named import
import Footer from './components/Footer'; // Default import

const App = () => {
  return (
    <div>
      <Header />
      {/* Content goes here */}
      <Footer />
    </div>
  );
};

export default App;

Conclusion

For further reading and more advanced topics, refer to the official React documentation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.