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.
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.
Functional Component with Props
Props in React allow functional components to receive data and behave dynamically.
Props are passed from parent components to child components.
They help make components reusable and customizable.
Data in a component changes based on the props it receives.
Output:
Hello, Alice!
When to Use ReactJS Functional Components
Functional components should be used whenever possible because they are simpler, easier to read, test, and maintain. With the introduction of React Hooks, functional components can now handle state and lifecycle features that were previously only available in class components.
State management: Functional components can use the useState Hook to manage state, so class components are no longer required for stateful logic.
Lifecycle methods: Hooks like useEffect can replace lifecycle methods such as componentDidMount, componentDidUpdate, and componentWillUnmount.
Modern best practice: Functional components with Hooks are now the recommended and standard approach in React development.
Passing Props to a ReactJS Functional Component
Props are used to pass data from a parent component to a child component. Props are read-only and allow you to make a component dynamic by passing different values into it.
1. Passing a Single Prop to a React Functional Component:
Output
Hello World!
In the App.js component, the message prop is passed to the Greeting component.
In the Greeting.js component, props.message is used to access the passed prop and dynamically render it.
2. Destructuring Props
Instead of using props.name, you can destructure the props object directly in the function’s argument to make the code cleaner.
The Welcome component receives the name prop using destructuring in the function argument, making it easier to access the name value.
Instead of writing props.name, we now directly use { name } within the component.
React Functional Components Vs Class Components
Functional Components
Class Components
Simple function with Hooks
ES6 classes extending React Component.
Requires Hooks like useState
Managed via this.state and this.setState.
Managed via useEffect
Dedicated lifecycle methods like componentDidMount.