Day 5 pf Mastering React Context API

ยท

3 min read

Day 5 pf Mastering React Context API

Introduction

In the realm of React development, managing state across components efficiently is crucial for building scalable and maintainable applications. While props drilling and state lifting have been traditional solutions, React Context API offers a more elegant and streamlined approach to sharing state across components without the need for prop drilling.

In this comprehensive guide, we'll delve into the nuances of React Context API, exploring its core concepts, implementation details, and best practices. Additionally, we'll take it a step further by integrating local storage with Context API to persist state across page reloads, enhancing the user experience and application robustness.

Overview of React.js

Understanding React Context API

React Context API provides a way to pass data through the component tree without having to pass props down manually at every level. It consists of three main components:

  1. Provider: It allows consuming components to subscribe to context changes.

  2. Consumer: It enables consuming components to subscribe to context changes.

  3. Context: It's the object created with React.createContext(). It represents the data that will be shared across the component tree.

Let's start by creating a simple example to demonstrate how Context API works:

import React from 'react';
const ThemeContext = React.createContext('light');
export default ThemeContext;
// App.js
import React from 'react';
import ThemeContext from './ThemeContext';
import Toolbar from './Toolbar';
function App() {
  return (
    <ThemeContext.Provider value="dark">
      <Toolbar />
    </ThemeContext.Provider>
  );
}

export default App;
// Toolbar.js
import React from 'react';
import ThemeContext from './ThemeContext';
function Toolbar() {
  return (
    <ThemeContext.Consumer>
      {theme => <div>The current theme is: {theme}</div>}
    </ThemeContext.Consumer>
  );
}

export default Toolbar;

In this example, App component wraps Toolbar component with ThemeContext.Provider, and Toolbar component consumes the value provided by ThemeContext.Provider using ThemeContext.Consumer.

Integrating Local Storage with React Context API

Integrating local storage with React Context API allows us to persist state even when the user refreshes the page or navigates away. This is particularly useful for preserving user preferences or application settings.

Let's extend our previous example to include local storage integration:

import React, { useState, useEffect } from 'react';
const ThemeContext = React.createContext();
export const ThemeProvider = ({ children }) => {
  const [theme, setTheme] = useState('light');

  useEffect(() => {
    const savedTheme = localStorage.getItem('theme');
    if (savedTheme) {
      setTheme(savedTheme);
    }
  }, []);

  const toggleTheme = () => {
    const newTheme = theme === 'light' ? 'dark' : 'light';
    setTheme(newTheme);
    localStorage.setItem('theme', newTheme);
  };

  return (
    <ThemeContext.Provider value={{ theme, toggleTheme }}>
      {children}
    </ThemeContext.Provider>
  );
};

export default ThemeContext;
// App.js
import React from 'react';
import { ThemeProvider } from './ThemeContext';
import Toolbar from './Toolbar';
function App() {
  return (
    <ThemeProvider>
      <Toolbar />
    </ThemeProvider>
  );
}

export default App;

In this updated example, we've created a ThemeProvider component that wraps the entire application. It manages the theme state using useState hook and syncs it with local storage using useEffect. The toggleTheme function allows toggling between light and dark themes, updating both state and local storage accordingly.

Conclusion

React Context API provides a powerful mechanism for managing global state in React applications, reducing the need for prop drilling and making state management more intuitive. By integrating local storage, we can further enhance the capabilities of Context API, enabling seamless persistence of state across page reloads and enhancing the user experience. With this newfound knowledge, you're well-equipped to leverage React Context API and local storage in your projects to build more robust and user-friendly applications.

Thank you for joining me on this journey I hope you found this blog insightful and empowering as you continue to dive deeper into the world of React development. Remember, learning is a continuous process, and I'm here to support you every step of the way

Connection :- Twitter or LinkedIn

ย