Explore the fundamentals of React Native hooks, including useState, useEffect, and custom hooks, with practical scenarios and clear explanations. This quiz enhances your understanding of state management, side effects, and reusable logic within React Native functional components.
Which value is set as the initial state when using useState like this: const [counter, setCounter] = useState(5)?
Explanation: Setting useState(5) initializes the state variable counter with the value 5. The number 0 would only be the default if 0 was passed instead. 'undefined' appears if you provide no initial value, but here 5 is supplied. 'counter' is simply the variable name and not the value.
Given useEffect(() =u003E { /* logic */ }, [count]), when does the effect run?
Explanation: The effect runs whenever the value of count changes because it is listed in the dependency array. It does not run on every render unless the dependency array is omitted. Running only once would use an empty array. It will always run when the dependency matches change, so 'Never' is incorrect.
After const [text, setText] = useState('Hello'), what does setText('World') do?
Explanation: Calling setText('World') updates the text state to 'World'. It does not remove the value, or set it to the string 'text', and it indeed changes the state rather than having no effect.
Why is the dependency array important in useEffect?
Explanation: The dependency array tells useEffect when to execute the effect by tracking changes in its listed values. It does not store all state, set initial state, or block component mounting. The other options do not represent the purpose of the dependency array.
What must a custom hook's name start with to be recognized as a hook?
Explanation: Custom hooks must start with 'use' to be identified properly as hooks in React conventions. Options like 'get' and 'set' are standard function prefixes but not for hooks. 'hook' is not a recognized prefix for custom hooks.
Is it allowed to use multiple useEffect hooks in a single functional component?
Explanation: You can use multiple useEffect hooks in one component, each handling different logic. Limiting to one or two is incorrect, and they do not need to run the same code. This allows clear separation of concerns within a component.
What happens in a component after calling setState from useState?
Explanation: Calling setState with useState causes the component to re-render using the new state value. It does not trigger a re-render for all components, only those affected. State is updated and previous state can still be accessed via the setter function if needed.
When creating a custom hook that manages user data, what is the usual return value?
Explanation: Custom hooks generally return state values along with functions to update or interact with that state. They do not return new components or class component instances. The term 'hook component' is misleading and not a valid return.
What happens if useEffect is given an empty dependency array?
Explanation: An empty dependency array causes useEffect to run only once after the initial component render. If you want the effect to run every time, omit the array. It does not prevent the effect from ever running or trigger it on any state change.
Can useState be used directly within class components?
Explanation: useState is designed for functional components and cannot be used in class components. Importing it, using special syntax, or applying it to all types are not valid approaches for class components, which use their own state management methods.