Browse Courses

Function Component

This document explains how props and event handling work in React function components, covering data flow, default props, customization, and conditional rendering.

This document explores how React function components use props to pass data, set default values, and handle events. It covers the principles of reusability, unidirectional data flow, customization, and composition, as well as practical event handling and conditional rendering techniques.


Working with Function Components – Props and Event Handling

Function components in React use properties, known as props, to pass data from parent to child components. Props are similar to objects and store the attributes of a component. They are immutable, meaning their values cannot be changed by the child component.

Example: Basic Function Component

1function Welcome() {
2  return <h1>Hello, World!</h1>
3}

Passing Data with Props

Props allow data to flow from a parent to a child component. For example, the App component can pass a name attribute to an EmployeeData child component. Inside the child, the props object is accessed using dot notation (e.g., props.name).

1function EmployeeData(props) {
2  return <h1>Employee Name: {props.name}</h1>
3}
4
5function App() {
6  return <EmployeeData name="John" />
7}

Props can be passed as static values or dynamically using curly braces. For instance, a parent can define a variable and pass it as a prop, or assign a static value directly in the JSX.

Default Props

Default props provide fallback values for a component if the parent does not supply them. These are defined in the child component and ensure consistent rendering even when some props are missing.

1function Department({ deptName = "Human Resources" }) {
2  return <div>Department: {deptName}</div>;
3}
4
5// Usage
6<Department /> // Renders: Department: Human Resources
7<Department deptName="Engineering" /> // Renders: Department: Engineering

Principles of Props and Function Components

  • Reusability: Components can be reused with different props, reducing code duplication.
  • Customization: Props allow components to adapt their behavior and appearance based on the data provided.
  • Unidirectional Data Flow: Data always flows from parent to child, making the application predictable and easier to debug.
  • Composition: Props enable the building of complex UIs by composing simpler components.

Event Handling and State in Function Components

React’s useState hook manages state within function components. State variables can control UI behavior, such as showing or hiding elements. Event handlers, like onClick, update state and trigger UI updates.

Example: Event Handling and useState

 1import { useState } from 'react'
 2
 3function SalaryIncrease({ increase }) {
 4  const [showIncrease, setShowIncrease] = useState(false)
 5
 6  return (
 7    <div>
 8      <button onClick={() => setShowIncrease(true)}>
 9        Display Annual Salary Increase
10      </button>
11      {showIncrease && <div>Increase: {increase}</div>}
12    </div>
13  )
14}

For example, a button click can set a boolean state variable to true, causing the component to conditionally render additional information. Conditional rendering is often implemented using logical expressions (e.g., &&).


Function Components with Arrays

Function Component with Array and DomJobCode Example
function keywordstarts with function keywordimport React from 'react' function Foo() { return ( <> <p>This is paragraph</p> </> ) } export default Foo
arrow functionstarts with const or let and uses =>import React from 'react' const Foo = () => { return ( <> <p>This is paragraph</p> </> ) } export default Foo
inline functiondefined within the JSXimport React from 'react' const Foo = () => { return ( <> <p>This is paragraph</p> </> ) } export default Foo
Propspassed to the component as attributesimport React from 'react' import ChildComponent from './ChildComponent' function ParentComponent () { let title='Project Manager'; return ( <> <ChildComponent title={title}/> </> ) } export default ParentComponent

Conclusion

React function components use props for data flow and customization, and state for managing dynamic UI behavior. By following best practices—such as not mutating props or state directly and using event handlers to update state—developers can create robust, reusable, and maintainable components.


FAQ

  1. Props allow components to receive and display dynamic data from their parent.
  2. Props are used to manage internal state.
  3. Props are only for styling components.
  4. Props are used to define static content.
(1) Props allow components to receive and display dynamic data from their parent, enabling flexible and reusable components.

The prop will not change, and React will not re-render the component. Props are immutable in the child component.

  1. Default props provide fallback values if the parent does not supply them.
  2. Default props are defined in the child component.
  3. Default props can be changed by the child component at runtime.
  4. Default props ensure consistent rendering.
(3) Default props cannot be changed by the child component at runtime; they are set as fallback values only.

Unidirectional data flow makes it easier to track how data moves through the app, making debugging and maintenance simpler.

Props are used for passing data from parent to child, while state is used for managing dynamic data within a component.

ConceptDescription
A. Props1. Manages dynamic data within a component
B. State2. Passes data from parent to child
C. useState3. Updates state and triggers re-render
A-2, B-1, C-3.

Ensure that props are not being mutated directly and that state is updated using the setter function.

Props in React function components are immutable and cannot be changed by the child component.

True. Props are read-only in the child component and must not be mutated.

  1. Reusability
  2. Customization
  3. Bidirectional data flow
  4. Composition
(3) Bidirectional data flow is not a principle of props; React uses unidirectional data flow.

Event handling allows components to respond to user actions and update state, enabling dynamic and interactive UIs.

Check if the state variable controlling the rendering is being updated correctly using the setter function.