Lifting state up means moving shared data to a common parent so multiple parts can use it. It helps keep your app organized and data consistent.
Lifting state up in React Native
import React, { useState } from 'react'; function Parent() { const [sharedData, setSharedData] = useState(''); return ( <> <Child1 data={sharedData} onChange={setSharedData} /> <Child2 data={sharedData} /> </> ); }
The parent component holds the state using useState.
It passes the state and a function to update it down to children via props.
text state and passes it with a setter to Child.import React, { useState } from 'react'; function Parent() { const [text, setText] = useState(''); return <Child input={text} onInputChange={setText} />; }
count state with two children: one to change it, one to show it.import React, { useState } from 'react'; function Parent() { const [count, setCount] = useState(0); return ( <> <Incrementer count={count} onIncrement={() => setCount(count + 1)} /> <Display count={count} /> </> ); }
This app has a parent component holding the name state. The Input child lets the user type their name. The Greeting child shows a message using that name. Both children share the same state lifted up to the parent.
import React, { useState } from 'react'; import { View, Text, TextInput, StyleSheet } from 'react-native'; function Parent() { const [name, setName] = useState(''); return ( <View style={styles.container}> <Input name={name} onNameChange={setName} /> <Greeting name={name} /> </View> ); } function Input({ name, onNameChange }) { return ( <TextInput style={styles.input} placeholder="Enter your name" value={name} onChangeText={onNameChange} accessibilityLabel="Name input" /> ); } function Greeting({ name }) { return <Text style={styles.greeting}>Hello, {name || 'stranger'}!</Text>; } const styles = StyleSheet.create({ container: { padding: 20, marginTop: 50 }, input: { borderColor: '#888', borderWidth: 1, padding: 10, fontSize: 18, borderRadius: 5 }, greeting: { marginTop: 20, fontSize: 24, fontWeight: 'bold' } }); export default Parent;
Always lift state up to the closest common parent of components that need it.
Passing state down via props keeps data flow clear and predictable.
Use descriptive prop names like onChange or onNameChange for callback functions.
Lifting state up means moving shared data to a parent component.
This helps multiple components stay in sync and share data easily.
Use props to pass state and update functions down to children.
Practice
lifting state up in React Native?Solution
Step 1: Understand the concept of shared state
Lifting state up means moving state to a common parent so multiple children can use it.Step 2: Identify the purpose of lifting state up
This helps keep data consistent and allows children to communicate through the parent.Final Answer:
To move shared state to a common parent component so children can access it -> Option BQuick Check:
Lifting state up = shared parent state [OK]
- Thinking each child should have its own separate state
- Believing lifting state up removes the need for props
- Confusing lifting state up with component styling
Solution
Step 1: Identify what to pass as prop
The parent passes a function to update state, not the state value or setState method directly.Step 2: Choose the correct syntax
PassingupdateStateFunctionas a prop is correct to allow child to call it.Final Answer:
<Child updateState={updateStateFunction} /> -> Option CQuick Check:
Pass update function as prop = <Child updateState={updateStateFunction} /> [OK]
- Passing the entire state object instead of a function
- Passing setState method directly without binding
- Passing state value instead of update function
Child is pressed?
function Parent() {
const [count, setCount] = React.useState(0);
return (
<>
<Child count={count} increment={() => setCount(count + 1)} />
<Text>Count: {count}</Text>
</>
);
}
function Child({ count, increment }) {
return <Button title={`Add`} onPress={increment} />;
}Solution
Step 1: Understand state and props flow
The Parent holds the count state and passes it plus an increment function to Child.Step 2: Analyze what happens on button press
Pressing the button calls increment, which updates count by adding 1.Final Answer:
The count displayed will increase by 1 each time the button is pressed -> Option DQuick Check:
State update via lifted function increments count [OK]
- Thinking count won't update because it's passed as prop
- Assuming setCount is not called correctly
- Confusing local and lifted state
function Parent() {
const [text, setText] = React.useState('');
return <Child text={text} setText={setText()} />;
}
function Child({ text, setText }) {
return <TextInput value={text} onChangeText={setText} />;
}Solution
Step 1: Check how setText is passed as prop
In Parent,setText={setText()}calls setText immediately instead of passing the function.Step 2: Correct way to pass setText
It should besetText={setText}to pass the function reference.Final Answer:
setText is called immediately instead of passing the function -> Option AQuick Check:
Pass function reference, not call it [OK]
- Calling setState function instead of passing it
- Confusing state value with update function
- Ignoring React hooks rules
TemperatureInputCelsius and TemperatureInputFahrenheit, that need to share temperature data. How should you use lifting state up to keep both inputs in sync?Solution
Step 1: Identify the need for shared state
Both siblings need to reflect the same temperature, so state must be shared.Step 2: Apply lifting state up
Move temperature state to their common parent and pass it plus update functions down as props.Step 3: Avoid incorrect approaches
Separate states cause inconsistency; global variables break React's data flow.Final Answer:
Move the temperature state to their common parent and pass value and update functions as props -> Option AQuick Check:
Lift shared state to parent for syncing siblings [OK]
- Keeping separate states causing out-of-sync inputs
- Trying to share state via global variables
- Passing state only to one sibling
Ready to prove it?
Pass the test and this pattern moves unknown
