React / Next.js

React Hooks

useState, useEffect, useContext, useRef, useCallback, useMemo, custom hooks

22 interview questionsยท
Junior
1

What is the useState hook in React?

Answer

useState is a React hook that manages local state in a functional component. Unlike regular JavaScript variables that are reset on every render, useState preserves the state value between successive renders. This hook returns an array containing the current state value and a function to update it, automatically triggering a component re-render when the state changes.

2

What is the correct syntax to declare state with useState?

Answer

The correct syntax uses array destructuring to extract the state value and its update function. By convention, the update function is prefixed with 'set' followed by the state variable name in camelCase. This convention improves code readability and is widely adopted in the React community. The order of elements in the array returned by useState is always the same: current value first, update function second.

3

What happens when calling a state update function?

Answer

When a state update function is called, React schedules a new component render with the new state value. The update is not immediate but asynchronous, meaning the state value is not modified instantly after the call. React batches multiple state updates to optimize performance and avoid triggering unnecessary renders. This batching strategy significantly improves application performance.

4

How to update state based on its previous value?

5

Can multiple useState hooks be used in the same component?

+19 interview questions

Master React / Next.js for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free