What is a React Component?
React components are independent units of code that serve as JavaScript functions. There are two types of components in React.js.
- Functional Component
- Class Component
A class component is a set of methods that offer functionality to an application, whereas a functional component is a JavaScript-like function that provides React JSX (a hybrid of HTML and JavaScript) as output during the execution.
A component can return anything from a simple HTML element with some inside content to a comprehensive logic-filled UI and functionality.
Below example shows one Functional component:
const App = () => {
return (
Hello React from localhost
Welcome to React, Best frontend Framework
);
};
In React, we can render the component inside another component. To do so, we wrap that component inside angle brackets like React element inside another component as shown:
const Greetings = ({ msg }) => {
return{msg}
;
};
const App = () => {
return ;
};
We can render the same component several times. When we render a React component as a React element, we generate an instance of that component.
const Greetings = ({ text }) => {
return{text}
;
};
const App = () => {
return (
<>
>
);
};
It should be noted that the React component is only declared once. However, we can use the same component multiple times as React Element in JSX. Every time React element is used it becomes the instance of that component.