Use the React Context API for managing global state

The React Context API can be a useful tool for managing global state in your application, especially if you have a large, complex component tree and need to avoid prop drilling.

Published on

7 min read

Feature Image

Introduction

The React Context API is a way to share values across a tree of components without having to pass props down manually at every level. It can be helpful for managing global state, such as user authentication or theme, because it allows you to avoid prop drilling and keep your component tree more organized and efficient.

But why ?

Here are some benefits of using the React Context API for global state management:

Simplifies prop drilling:

When you have a global state that needs to be accessed by multiple components, you would normally have to pass it down through multiple levels of components using props. This can get cumbersome and can make your component tree more difficult to understand. The Context API allows you to avoid this by providing a way to share values across your entire component tree without having to pass props down manually.

Improves code organization:

Using the Context API can help you keep your code more organized by allowing you to centralize your global state in a single location, rather than having it scattered throughout your component tree. This can make it easier to understand how your global state is being used and modified.

Improves performance:

Because the Context API allows you to avoid prop drilling, it can also improve the performance of your application by reducing the number of props that need to be passed down through the component tree. This can be especially helpful if you have a large, complex application with many levels of components.

Code snippet comparision

Here is an example of prop drilling in a React application:

const App = () => {
  const [theme, setTheme] = React.useState('light')

  return (
    <div className={`theme-${theme}`}>
      <Header theme={theme} setTheme={setTheme} />
      <Content theme={theme} />
    </div>
  )
}

const Header = (props) => {
  const { theme, setTheme } = props

  return (
    <header className={`theme-${theme}`}>
      <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>Toggle theme</button>
    </header>
  )
}

const Content = (props) => {
  const { theme } = props

  return <div className={`theme-${theme}`}>Content</div>
}

In this example, the theme and setTheme values are passed down from the App component to the Header and Content components using props. This is an example of prop drilling because the theme value has to be passed through multiple levels of components in order to be accessed by the Content component.

Here is the equivalent code using the context api:

const ThemeContext = React.createContext()

const App = () => {
  const [theme, setTheme] = React.useState('light')

  return (
    <ThemeContext.Provider value={{ theme, setTheme }}>
      <Layout />
    </ThemeContext.Provider>
  )
}

const Layout = () => {
  const { theme } = React.useContext(ThemeContext)

  return (
    <div className={`theme-${theme}`}>
      <Header />
      <Content />
    </div>
  )
}

const Header = () => {
  const { theme, setTheme } = React.useContext(ThemeContext)

  return (
    <header className={`theme-${theme}`}>
      <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>Toggle theme</button>
    </header>
  )
}

const Content = () => {
  const { theme } = React.useContext(ThemeContext)

  return <div className={`theme-${theme}`}>Content</div>
}

Prop drilling can be inefficient and can make your component tree more difficult to understand, especially if you have a large, complex application with many levels of components.

The React Context API provides a way to avoid prop drilling by allowing you to share values across your entire component tree without having to pass props down manually.