Address
304 North Cardinal St.
Dorchester Center, MA 02124
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM
A React component is a reusable piece of UI that encapsulates logic and renders functions. Components allow you to break down a complex UI into smaller, independent, and reusable parts. That is why in React, a component is a self-contained piece of code that renders a specific part of a user interface. These are the building blocks of React applications, and they can be reused throughout an application.
Components are defined using JavaScript, a component can be written in two ways in React.
Class components are the older style of component, and they are defined using the <strong>class</strong>
keyword. Functional components are the newer style of component, and they are defined using the <strong>function</strong>
keyword.
Class-based components are created using the class keyword and extends the React.Component, which will then be able to use the functionality of React Component such as its lifecycle methods, state, and render(). Class-based components can be written in more complex stateful manners.
When you extend React.Component, you are inheriting from the React library’s base component class. This gives your component access to all of the methods and properties that are defined in React.Component, including:
<strong>lifecycle methods</strong>
: These methods are called at specific points in the component’s lifecycle, such as when it is first created or when it is updated.<strong>state</strong>
: This object is used to store data that can change over time.<strong>render()</strong>
: This method is responsible for returning the HTML that should be rendered for the component.Extending React.Component is a good practice because it makes your components more reusable and easier to maintain. It also gives you access to the powerful features that are built into the React library. Here is a class based component code example:
App.js
import React from "react";
class MyComponent extends React.Component {
render() {
return (
<div>
<h1>This is my class component</h1>
</div>
);
}
}
function App() {
return (
<div>
<MyComponent />
</div>
);
}
-----------------------
OUTPUT:
This is my class component
Functional components are simple JavaScript fucntions that take in ‘props’ as an argument and return a React element. They are defined using the ‘function’ keyword and can be written in a concise stateless manner. Here is a class based component code example:
import React from "react";
const MyComponent = () => {
return (
<div>
<h1>This is my functional component</h1>
</div>
);
};
function App() {
return (
<div>
<MyComponent />
</div>
);
}
-----------------------
OUTPUT:
This is my functional component
Advantages
Advantages
Disadvantages
Disadvantages
There is a situation after learning both types of React Component that which we should use while writing a code, Here are some additional considerations when choosing between class and functional components:
The component lifecycle is an important part of React, and one needs to understand it in order to be a good developer of React, to know What is Component Lifecycle, check out “React Component Lifecycle“.
Also checkout One-way data flow in ReactJS in 5 easy step
I hope this content will be enough to understand what is JSX and how it works with multiple examples. If your want to learn more about ReactJS, you can follow all the ReactJS tutorials starting from “Introduction to React” on devcribe.com. Happy Coding!