Complete the code to access the global state using React Context.
const value = useContext([1]);We use useContext with the context object (e.g., MyContext) to access global state.
Complete the code to provide global state to child components.
<MyContext.Provider value=[1]>
{children}
</MyContext.Provider>The value prop of the provider should be the global state to share it with children.
Fix the error in the component to avoid prop drilling by using global state.
function ChildComponent({ data }) {
const globalData = useContext([1]);
return <Text>{globalData}</Text>;
}Using useContext(DataContext) accesses global state directly, avoiding passing data through props.
Fill both blanks to create a global state and provide it to the app.
const [[1], setData] = useState('Hello'); <MyContext.Provider value=[2]> <App /> </MyContext.Provider>
The state variable is named data and the provider value should be data to share it globally.
Fill all three blanks to consume and update global state in a component.
const { [1], [2] } = useContext(MyContext);
function update() {
[2]('New Value');
}
return <Button onPress={update} title=[1] />;We destructure data and setData from context. The button title shows data.