React / Next.js

State Management with Context

Context API, createContext, useContext, Provider pattern, context composition, performance

18 interview questionsยท
Junior
1

What is the Context API in React?

Answer

Context API is a built-in React mechanism that allows sharing data between components without explicitly passing props through every level. It solves the prop drilling problem by creating global state accessible to all descendant components. Context is particularly suitable for rarely changed data like theme, language, or authenticated user information.

2

Which function is used to create a new Context?

Answer

The createContext function is React's native method to initialize a new context. It accepts an optional default value that will be used if a component consumes the Context without a parent Provider. This function returns an object containing Provider and Consumer, although Consumer is rarely used with modern hooks.

3

Which hook is used to consume a Context value?

Answer

The useContext hook allows child components to read the value provided by the nearest Provider in the component tree. It replaces the older Consumer API and offers a more concise and readable syntax. The component will automatically resubscribe to Context changes and update accordingly.

4

Which component is used to provide a value to a Context?

5

What happens if a component uses useContext without a parent Provider?

+15 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