Chapter 3: Sharing Component State with Context

React has provided Context since version 16.3. Context has nothing to do with states, but it's a mechanism for passing data from component to component instead of using props. By combining Context with a component state, we can provide a global state.

In addition to the Context support provided since React 16.3, React 16.8 introduced the useContext hook. By using useContext and useState (or useReducer), we can create custom hooks for a global state.

Context is not fully designed for global states. One of the known limitations is that all Context consumers re-render upon updates, which can lead to extra re-renders. It's a general recommendation to split a global state into pieces.

In this chapter, ...

Get Micro State Management with React Hooks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.