10 ReactJS Function Components
10 ReactJS Function Components
Components
Function Components
• React Function Components, also known as React Functional
Components.
• They are simple, lightweight, and powerful tools for rendering UI and
handling logic.
• Functional components can accept props as input and return JSX that
describes what the component should render.
• When a functional component receives input and is rendered, React
uses props and updates the virtual DOM to ensure the UI reflects the
current state.
Function Components
• Props: Functional components receive input data through props,
which are objects containing key-value pairs.
• Processing Props: After receiving props, the component processes
them and returns a JSX element that defines the component's
structure and content.
• Virtual DOM: When the component is rendered, React creates a
virtual DOM tree that represents the current state of the application.
• Re-rendering: If the component's props or state change, React
updates the virtual DOM tree accordingly and triggers the component
to re-render.
Function Components
import React from 'react';
function App() {
const greeting = 'Hello Function Component!';
return <h1>{greeting}</h1>;
}
• Each time React needs to render <App />, it calls the function again.
• That means everything inside App() is re-executed on every render.
Variables function CounterFunction() {
let count = 0; // local variable
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me
</button>
</div>
); }
}
ReactJS Hooks
• Introduced in React 16.8, are among the most impactful updates to
the library
• They let developers use state, side effects, and other React features
without writing class components.
• Hooks are special functions that let you “hook into” React features
(like state and lifecycle methods) in functional components.
• State hooks, specifically useState and useReducer, allow functional
components to manage state.
• They provide an easier and cleaner approach to managing
component-level states in comparison to class components.
ReactJS Hooks import { useState } from "react";
• Named Export
• You can export multiple things from the same file.
// Parent Component
const App = () => {
return <Welcome name="Jiya" />;
};
Handling Input fields
return (
export default function App() { <div>
<h2>Single Input Example</h2>
// single state variable for username
const [username, setUsername] = useState(""); <input
type="text"
// update username when user types placeholder="Enter Username"
const handleChange = (e) => { value={username}
setUsername(e.target.value); onChange={handleChange}
}; />
<p>Username: {username}</p>
</div>
);
}
What is ‘e’ in handling function
• When you attach an event handler in React (e.g., onChange={handleChange}),
• React automatically passes an event object to your function.
• This object is usually named e (short for event).
• Inside an event, target is the element that triggered the event.
• If you type into <input>, then e.target is that <input> element.
• e.target // refers to the input DOM element
• e.target.value // gives you whatever the user typed into the input
Handling Input Fields
• <input type="text" onChange={(e) => console.log(e.target.value)} />
• You type Hello into the box:
• First keystroke: console shows H
• Next: He
• Then: Hel
• Then: Hell
• Finally: Hello
• Because each time you type, the event fires
• React passes e (event)
• e.target is the input and .value is the latest input text.
Handling Inputs
• const [username, setUsername] = useState("");
• Why do we use const for usename instead of let or var?
• useState always gives you exactly two things:
• the current state value (username)
• the updater function (setUsername)
• This pair will not change identity during the lifetime of the
component.
• You’ll always have the same setUsername function.
• username is replaced with a new value only when React re-
renders, not by reassignment.
Handling Inputs
• const [username, setUsername] = useState("");
• const mean cannot reassign the variable identifier.
• But if the const holds a reference to an object or function, the reference itself
is constant, but the internal data can change.
• const user = { name: "Alice", age: 25 };