Lifting State Up
Lifting state up is a technique in React where the state management is moved from a child component to a parent component in order to share data between multiple child components.
To understand lifting state up, let's consider an example. Imagine we have a parent component called TemperatureConverter
that consists of two child components: CelsiusInput
and FahrenheitInput
.
The CelsiusInput
component takes a temperature in Celsius as an input, and the FahrenheitInput
component takes a temperature in Fahrenheit as an input.
The goal is to allow the user to input a temperature in one component and automatically update the other component with the converted temperature.
To accomplish this, we need to lift the state of the temperatures from the child components to the parent component. The parent component will handle the conversion logic and update the state accordingly.
Here's an example of how we can implement this in React:
1import React, { useState } from 'react';
2
3function TemperatureConverter() {
4 const [celsius, setCelsius] = useState(0);
5 const [fahrenheit, setFahrenheit] = useState(0);
6
7 const handleCelsiusChange = (event) => {
8 const value = event.target.value;
9 setCelsius(value);
10 setFahrenheit((value * 9) / 5 + 32);
11 };
12
13 const handleFahrenheitChange = (event) => {
14 const value = event.target.value;
15 setFahrenheit(value);
16 setCelsius(((value - 32) * 5) / 9);
17 };
18
19 return (
20 <div>
21 <h2>Lifting State Up</h2>
22 <div>
23 <label>Celsius:</label>
24 <input type="number" value={celsius} onChange={handleCelsiusChange} />
25 </div>
26 <div>
27 <label>Fahrenheit:</label>
28 <input type="number" value={fahrenheit} onChange={handleFahrenheitChange} />
29 </div>
30 </div>
31 );
32}
33
34export default TemperatureConverter;
In this example, the TemperatureConverter
component holds the state for both the celsius
and fahrenheit
values using the useState
hook. The component defines two event handlers, handleCelsiusChange
and handleFahrenheitChange
, to update the state based on user input.
The CelsiusInput
and FahrenheitInput
components receive the values as props and display them in their respective input fields. When the user changes the value in one input field, the corresponding event handler updates the state, which triggers a re-render of both input fields.
By lifting the state up to the TemperatureConverter
component, we have centralized the state management and ensured that both child components always display the correct converted temperature value.
Lifting state up is useful in situations where multiple components need access to the same data or when you need to synchronize the state between components. It promotes reusability and makes the code easier to understand and maintain.
xxxxxxxxxx
}
// Lifting State Up
// Lifting state up is the process of moving the state
// management from a child component to a parent component
// in order to share the data between multiple child components.
// Let's say we have a parent component called `TemperatureConverter`
// that consists of two child components: `CelsiusInput`
// and `FahrenheitInput`.
// The `CelsiusInput` component takes a temperature in Celsius
// as an input and the `FahrenheitInput` component takes a temperature
// in Fahrenheit as an input.
// The goal is to allow the user to input a temperature in one input field
// and automatically update the other input field with the converted temperature.
// To achieve this, we need to lift the state of the temperatures
// from the child components to the parent component and have the parent
// component handle the conversion logic and update the state.
// Here's an example of how this can be implemented:
import React, { useState } from 'react';
function TemperatureConverter() {
const [celsius, setCelsius] = useState(0);
const [fahrenheit, setFahrenheit] = useState(0);