Complete the code to create a state variable using React Native hooks.
const [count, [1]] = useState(0);
The useState hook returns a pair: the current state and a function to update it. The convention is to name the update function starting with 'set'.
Complete the code to update the state variable when a button is pressed.
const increment = () => [1](prev => prev + 1);
To update the state, you call the update function returned by useState. Here, setCount is called with a function that increments the previous value.
Fix the error in the code to correctly use the useReducer hook for state management.
const [state, [1]] = useReducer(reducer, initialState);setState instead of dispatch with useReducer.When using useReducer, the second element is the dispatch function used to send actions to the reducer.
Fill both blanks to create a context provider and consume the context in a child component.
const MyContext = createContext();
function MyProvider({ children }) {
const [value, [1]] = useState('Hello');
return <MyContext.Provider value=[2]>{children}</MyContext.Provider>;
}The provider needs to pass the state and the update function as an object to allow consumers to read and update the context value.
Fill all three blanks to correctly use the useContext hook inside a functional component.
function MyComponent() {
const context = useContext([1]);
return <Text onPress={() => [2]('New Value')}>{context.[3]</Text>;
}Inside the component, useContext is called with the context object. Then you can use the setter function to update the value and display the current value.