Design Patterns · Middle · Question 18 of 75

How do you inject a dependency into React components so you can swap the API client in tests?

Explanation

Context is React's built-in DI mechanism: <ApiProvider client={mockClient}> in tests and Storybook, the real client in the app. QueryClientProvider and the Redux store provider work the same way.

Further reading

Problem with a question

Design Patterns · Middle · question 18

Test yourself

Take the Design Patterns · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz