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.
Example 2: This example demonstrates the use of useState() hook in functional component.
Javascript
// Filename - index.js import React from "react" ; import ReactDOM from "react-dom" ; import Example from "./App" ; const root = ReactDOM.createRoot( document.getElementById( "root" ) ); root.render( <React.StrictMode> <Example /> </React.StrictMode> ); |
Javascript
// Filename - App.js 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 w3wiki</h1> ) : ( <h1>A Computer Science Portal for Geeks</h1> )} </div> ); }; export default Example; |
Step to run the application: Open the terminal and type the following command.
npm start
Output: Open the browser and our project is shown in the URL http://localhost:3000/
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.
Example 3: This example demonstrates the use of useEffect() hook.
Javascript
// Filename - index.js import React from "react" ; import ReactDOM from "react-dom" ; import Example from "./App" ; const root = ReactDOM.createRoot( document.getElementById( "root" ) ); root.render( <React.StrictMode> <Example /> </React.StrictMode> ); |
Javascript
// Filename - App.js import React, { useEffect } from "react" ; const Example = () => { useEffect(() => { console.log( "Mounting..." ); }); return <h1>Geeks....!</h1>; }; export default Example; |
Step to run the application: Open the terminal and type the following command.
npm start
Output: Open the browser and our project is shown in the URL http://localhost:3000/
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.
Example 4: This example demonstrates the use of props.
Javascript
// Filename - index.js import React from "react" ; import ReactDOM from "react-dom" ; import PropsExample from "./App" ; const root = ReactDOM.createRoot( document.getElementById( "root" ) ); root.render( <React.StrictMode> <PropsExample /> </React.StrictMode> ); |
Javascript
// Filename - App.js import React, { useState } from "react" ; const Example = (props) => { return <h1>{props.data}</h1>; }; const PropsExample = () => { // const [change, setChange] = useState(true); const [change, setChange] = useState( false ); return ( <div> <button onClick={() => setChange(!change)}> Click Here! </button> {change ? ( <Example data= "Welcome to w3wiki" /> ) : ( <Example data= "A Computer Science Portal for Geeks" /> )} </div> ); }; export default PropsExample; |
Step to run the application: Open the terminal and type the following command.
npm start
Output: Open the browser and our project is shown in the URL http://localhost:3000/
ReactJS Functional Components
Functional Component is one way to create components in a React Application. React.js Functional Components helps to create UI components in a Functional and more concise way. In this article, we will learn about functional 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
Table of Content
- Functional components in React :
- Ways to call the functional component:
- Problem with using functional components
- Advantage of using hooks in functional components
Contact Us