0
0
React Nativemobile~10 mins

State management comparison in React Native - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a state variable using React Native hooks.

React Native
const [count, [1]] = useState(0);
Drag options to blanks, or click blank then click option'
AsetCount
BgetCount
CcountSetter
DupdateCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name that does not start with 'set'.
Confusing the state variable with the update function.
2fill in blank
medium

Complete the code to update the state variable when a button is pressed.

React Native
const increment = () => [1](prev => prev + 1);
Drag options to blanks, or click blank then click option'
Acount
BchangeCount
CupdateCount
DsetCount
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to update the state variable directly instead of using the update function.
Using the wrong function name.
3fill in blank
hard

Fix the error in the code to correctly use the useReducer hook for state management.

React Native
const [state, [1]] = useReducer(reducer, initialState);
Drag options to blanks, or click blank then click option'
Adispatch
BsetState
CupdateState
DchangeState
Attempts:
3 left
💡 Hint
Common Mistakes
Using setState instead of dispatch with useReducer.
Confusing useReducer with useState syntax.
4fill in blank
hard

Fill both blanks to create a context provider and consume the context in a child component.

React Native
const MyContext = createContext();

function MyProvider({ children }) {
  const [value, [1]] = useState('Hello');
  return <MyContext.Provider value=[2]>{children}</MyContext.Provider>;
}
Drag options to blanks, or click blank then click option'
AsetValue
Bvalue
C{ value, setValue }
DupdateValue
Attempts:
3 left
💡 Hint
Common Mistakes
Passing only the state value without the setter function.
Using incorrect variable names.
5fill in blank
hard

Fill all three blanks to correctly use the useContext hook inside a functional component.

React Native
function MyComponent() {
  const context = useContext([1]);
  return <Text onPress={() => [2]('New Value')}>{context.[3]</Text>;
}
Drag options to blanks, or click blank then click option'
AMyContext
BsetValue
Cvalue
DOtherContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong context object in useContext.
Trying to update the state variable directly instead of using the setter.