DEV Community

Cover image for React Components Basic 101
AricayaJohn
AricayaJohn

Posted on

React Components Basic 101

The Building Blocks Of Modern Web Application

What is a React Component:
-Components are part of React, which is a JavaScript Library used to build web interface.
-They are written in JSX, a syntax extension that combines JS and HTML making it more readable.

Analogy
Imagine codes inside LEGO blocks that, when pieced together, forms a web page application. Each webpage is broken up into pieces of smaller User Interface. By combining these blocks, you build complex and dynamic web pages.

Why is it important:
Reusability:
-You can create a component and use that same application through out different parts of your code.
-This means you write less code and reduce the potential bugs and errors
Maintainability:
-You can organize each component for a clear purpose and simplify the furture updates or modification.

But how does Component Work and Pieced together:

We have a main component that combines all the other components.
It is usually in the default component which is the App.js.
and in here we would see:

Import React from 'react';

function App() {
  return (
    <div>
      <Header />
      <Content />
      <Footer />
    </div>
  );
}

export default App
Enter fullscreen mode Exit fullscreen mode

This App component is linked to index.js, which renders it to the index.html file using:

ReactDOM.render(<App />, document.getElementById("root"));
Enter fullscreen mode Exit fullscreen mode

Each component is linked by the Import and Export
Import syntax:
import ComponentName from "./componentfolder/ComponentName";

To use a component, you import it at the beginning of the file

Export syntax:
export default ComponentName;

To make a component available for use in other parts of your application, you export it at the end of the file

What do we put in the middle?
-Component syntax:
We start with a component function and adds a return property that will contain an JSX style of code

function ComponentName () {
  return (....)
}
Enter fullscreen mode Exit fullscreen mode

Conclusion:
This simple introduction opens up a more complex structure and component lifecycle that we can explore. We can pass properties (props) to components and use that data to make our UI more interactive. We can nest components inside other components to create more sophisticated layouts.

For those up to the challenge, exploring class-based components can provide a deeper understanding of React. Class components are the original structure for writing React components and offer additional features like lifecycle methods.

React components are powerful tools that allow developers to build modular, maintainable, and reusable code. By mastering components, you can create efficient and dynamic web applications. Happy coding!

Top comments (0)