Creating Functional Components
In React, functional components are created as JavaScript functions. They are a simpler and more concise way of defining components compared to class components.
Functional components are especially useful when you only need to render UI based on the given props and don't require any internal state or lifecycle methods.
Here's an example of a functional component:
SNIPPET
1function Greeting() {
2 return (
3 <div>
4 <h2>Hello, World!</h2>
5 <p>This is a functional component.</p>
6 </div>
7 );
8}
In this example, the Greeting
component simply renders a div
element containing a heading and a paragraph. This component can be reused and rendered anywhere in your application.
With functional components, you can also utilize React hooks such as the useState
hook to introduce state management into your components.
xxxxxxxxxx
10
// In React, functional components are created as JavaScript functions
function Greeting() {
return (
<div>
<h2>Hello, World!</h2>
<p>This is a functional component.</p>
</div>
);
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment