State lifting is a technique in React where state is moved from child components to their common parent. This allows multiple components to share and manage the same state. State lifting is necessary when multiple components need to display the same data or when child components need to interact with each other.
Main reasons for state lifting:
State lifting is one of the fundamental concepts in React that helps manage application state at the parent component level. This technique allows for more predictable and maintainable code.
State lifting is necessary in the following cases:
When multiple components need the same data:
// Parent component with lifted state
function App() {
const [count, setCount] = useState(0);
return (
<div>
<CounterDisplay count={count} />
<CounterButton onClick={() => setCount(count + 1)} />
</div>
);
}When child components need to interact:
// Parent manages state of both child components
function Form() {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
return (
<form>
<NameInput value={name} onChange={setName} />
<EmailInput value={email} onChange={setEmail} />
</form>
);
}// Without state lifting - components are not synchronized
function BadExample() {
return (
<div>
<Counter />
<Counter />
</div>
);
}
// With state lifting - synchronized state
function GoodExample() {
const [count, setCount] = useState(0);
return (
<div>
<Counter count={count} setCount={setCount} />
<Counter count={count} setCount={setCount} />
</div>
);
}function UserForm() {
const [userData, setUserData] = useState({
name: '',
email: '',
age: ''
});
// All form fields are managed from one state
}// ❌ State lifting when not needed
function Parent() {
const [child1State, setChild1State] = useState('');
const [child2State, setChild2State] = useState('');
return (
<div>
<Child1 state={child1State} setState={setChild1State} />
<Child2 state={child2State} setState={setChild2State} />
</div>
);
}
// ✅ State remains in child components
function Parent() {
return (
<div>
<Child1 />
<Child2 />
</div>
);
}// ❌ Lifting all child component state
function Parent() {
const [childState, setChildState] = useState({
localData: '',
localPreference: '',
localUIState: false
});
return <Child state={childState} setState={setChildState} />;
}
// ✅ Only necessary state is lifted
function Parent() {
const [sharedData, setSharedData] = useState('');
return <Child sharedData={sharedData} setSharedData={setSharedData} />;
}State lifting is a standard React practice that works in all versions of the library.
State lifting is an important technique for creating predictable and maintainable React applications, allowing efficient management of shared state among multiple components.
What’s the difference between these two approaches and which is preferable?
// Approach 1: State in child components
function Parent() {
return (
<div>
<Input1 />
<Input2 />
<SubmitButton />
</div>
);
}
// Approach 2: State lifting
function Parent() {
const [value1, setValue1] = useState('');
const [value2, setValue2] = useState('');
return (
<div>
<Input1 value={value1} onChange={setValue1} />
<Input2 value={value2} onChange={setValue2} />
<SubmitButton
onSubmit={() => console.log({ value1, value2 })}
/>
</div>
);
}Answer: Approach 2 (with state lifting) is preferable.
Explanation:
Approach 1 (without lifting):
Approach 2 (with state lifting):
When to use each approach:
State lifting is especially important when working with forms, filters, sorting, and other scenarios where multiple components need to work with shared data.
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪