In this article, we will learn about funcitonal components in React, different ways to call the functional component and also learn how to create the functional components. We will also demonstrate the use of hooks in functional components
Functional components in React
ReactJS Functional components are some of the more common components that will come across while working in React. These are simply JavaScript functions. We can create a functional component in React by writing a JavaScript function. These functions may or may not receive data as parameters. In the functional Components, the return value is the JSX code to render to the DOM tree.
Ways to call the functional component:
We can call the functions in javascript in other ways as follows:
1. Call the function by using the name of the function followed by the Parentheses.
// Example of Calling the function with function name followed by Parentheses
import React from 'react';
import ReactDOM from 'react-dom/client';
function Parentheses() {
return (<h1>
We can call function using name of the
function followed by Parentheses
</h1>);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(Parentheses());
2. Call the function by using the functional component method.
// Example of Calling the function using component call
import React from 'react';
import ReactDOM from 'react-dom/client';
function Comp() {
return (<h1> As usual we can call the function using component call</h1>);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Comp />);
Now, We will use the functional component method to create a program and see how functional components render the component in the browser.
Program to demonstrate the creation of functional components
- Open your React project directory and go to the src folder then edit the index.js file.
Javascript
//index.js File import React from 'react' ; import ReactDOM from 'react-dom' ; import Demo from './App' ; ReactDOM.render( <React.StrictMode> <Demo /> </React.StrictMode>, document.getElementById( 'root' ) ); |
- Open the App.js file from the src folder and edit the file.
Javascript
//App.js File import React from 'react' ; import ReactDOM from 'react-dom' ; const Demo=()=>{ return <h1>Welcome to neveropen</h1>}; export default Demo; |
Output: After editing both files check the output in the browser. You will get the output as shown below.
Problem with using functional components
Functional components lack a significant amount of features as compared to class-based components and they do not have access to dedicated state variables like class-based components.
Advantage of using hooks in functional components
The problem discussed above is solved with the help of a special ReactJS concept called “hooks”. ReactJS has access to a special hook called useState(). The useState() is used to initialize only one state variable to multiple state variables. The first value returned is the initial value of the state variable, while the second value returned is a reference to the function that updates it.
Program to demonstrate the use of useState() hook
Filepath- src/index.js: Open your React project directory and edit the Index.js file from the src folder.
Javascript
//index.js File import React from 'react' ; import ReactDOM from 'react-dom' ; import Example from './App' ; ReactDOM.render( <React.StrictMode> <Example /> </React.StrictMode>, document.getElementById( 'root' ) ); |
Filepath- src/App.js: Open your React project directory and edit the App.js file from the src folder:
Javascript
//App.js File import React, { useState } from 'react' ; const Example = () => { const [change, setChange] = useState( true ); return ( <div> <button onClick={() => setChange(!change)}> Click Here! </button> {change ? <h1>Welcome to neveropen</h1> : <h1>A Computer Science Portal for Geeks</h1>} </div> ); } export default Example; |
Output: You will get the output as shown below.
Functional components do not have access to lifecycle functions like class-based components do since lifecycle functions need to be defined within the boundaries of a class. A special React hook called useEffect() needs to be used. It is worth noting that useEffect() isn’t an exact duplicate of the lifecycle functions – it works and behaves in a slightly different manner.
Program to demonstrate the use of useEffect() hook
- Filepath- src/index.js: Open your React project directory and edit the index.js file from the src folder.
Javascript
//index.js File import React from 'react' ; import ReactDOM from 'react-dom' ; import Example from './App' ; ReactDOM.render( <React.StrictMode> <Example /> </React.StrictMode>, document.getElementById( 'root' ) ); |
- Filepath- src/App.js: Open your React project directory and edit the App.js file from the src folder.
Javascript
//App.js File import React, { useEffect } from 'react' ; const Example = () => { useEffect(() => { console.log( "Mounting..." ); }); return ( <h1> Geeks....! </h1> ); } export default Example; |
Output: you will get the output like this in your browser.
Data is passed from the parent component to the child components in the form of props. ReactJS does not allow a component to modify its own props as a rule. The only way to modify the props is to change the props being passed to the child component. This is generally done by passing a reference of a function in the parent component to the child component.
Program to demonstrate the use of props
- Filepath- src/index.js: Open your React project directory and edit the Index.js file from the src folder.
Javascript
//index.js File import React from 'react' ; import ReactDOM from 'react-dom' ; import Example from './App' ; ReactDOM.render( <React.StrictMode> <Example /> </React.StrictMode>, document.getElementById( 'root' ) ); |
- Filepath- src/App.js: Open your React project directory and edit the App.js file from the src folder.
Javascript
import React, { useState } from 'react' ; import props from 'prop-types' ; const Example = () => { return ( <h1>{props.data}</h1> ); } function propsExample() { const [change, setChange] = useState( true ); return ( <div> <button onClick={() => setChange(!change)}> Click Here! </button> {change ? <Example data= "Welcome to neveropen" /> : <Example data= "A Computer Science Portal for Geeks" />} </div> ); } export default Example; |
Output: You will see the output like this in your browser.