DEV Community

Cover image for Exploring React State Management: From Simple to Sophisticated Solutions
Bhavesh Yadav
Bhavesh Yadav

Posted on

Exploring React State Management: From Simple to Sophisticated Solutions

Introduction

React state management plays a crucial role in building dynamic and interactive web applications and it is very important, infact most important thing you should know if you want to work with react.

While libraries like Redux are popular choices for managing application state, but you should understand when to use them and when to not and it's also important to consider simpler alternatives like the Context API when they suffice.

In this blog post, we'll explore multiple examples of state management in React, ranging from basic useState() to more advanced libraries like Redux, while highlighting the benefits of using simpler solutions like the Context API. Let's get started! 🚀

Basic State Management with useState()

We begin with the simplest form of state management using the useState() hook. We'll explore how to initialize and update state within a functional component. By demonstrating a straightforward example involving a button click counter, we highlight how useState() can be used to manage basic state needs effectively.

import React, { useState } from 'react';

const Counter = () => {
  const [count, setCount] = useState(0);

  const increment = () => {
    setCount(count + 1);
  };

  const decrement = () => {
    setCount(count - 1);
  };

  return (
    <div>
      <h1>Counter</h1>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
      <button onClick={decrement}>Decrement</button>
    </div>
  );
};

export default Counter;
Enter fullscreen mode Exit fullscreen mode

In the example above, we start by importing the useState hook from the react package. Inside the Counter component, we define a state variable count using the useState hook and initialize it with a value of 0.

The setCount function, also provided by useState, allows us to update the value of count and trigger a re-render of the component. It takes the new value as an argument.

We then define two functions, increment and decrement, which respectively increase and decrease the value of count using the setCount function. These functions are called when the corresponding buttons are clicked.

Finally, we render the current count value and the buttons for incrementing and decrementing the count.

With this example, we can easily manage and update the state of the count variable within the Counter component. Whenever the state changes, React will handle the re-rendering of the component and update the displayed count accordingly.

This basic example demonstrates the simplicity and power of the useState() hook for managing state in React applications.

Prop Drilling and the Context API

Next, we delve into the challenges of "prop drilling," where state needs to be passed through multiple components. To address this issue, we introduce the Context API. Through a practical example, we illustrate how the Context API enables us to share state across the component tree, eliminating the need for prop drilling.

import React, { createContext, useContext } from 'react';

// Create a context
const MyContext = createContext();

// Parent component
const Parent = () => {
  const value = 'Hello from Parent';

  return (
    <MyContext.Provider value={value}>
      <Child />
    </MyContext.Provider>
  );
};

// Child component
const Child = () => {
  const value = useContext(MyContext);

  return <p>{value}</p>;
};

export default Parent;
Enter fullscreen mode Exit fullscreen mode

In this example, we create a context using the createContext function from React. The MyContext object returned by createContext includes Provider and Consumer components.

In the Parent component, we define the value we want to share, in this case, "Hello from Parent". We wrap the Child component inside the Provider component and pass the value using the value prop.

In the Child component, we use the useContext hook to access the shared value from the context. We can directly access the value without passing it through props. In this case, the value will be "Hello from Parent".

Redux for Centralized State Management

Moving to more complex scenarios, we introduce Redux, a widely adopted library for managing application state. With a step-by-step example, we demonstrate how Redux can be integrated into a React application to handle state changes effectively.

Note that this is the example of classic redux and no one uses it today but instead we use something called redux toolkit and its same concepts as redux but a lot easier and in our next blog we will completely understand redux toolkit and how it makes our lives easier.

import React from 'react';
import { createStore } from 'redux';
import { Provider, useSelector, useDispatch } from 'react-redux';

// Define an initial state
const initialState = {
  count: 0,
};

// Define actions
const increment = () => ({
  type: 'INCREMENT',
});

const decrement = () => ({
  type: 'DECREMENT',
});

// Define reducer
const reducer = (state = initialState, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return { ...state, count: state.count + 1 };
    case 'DECREMENT':
      return { ...state, count: state.count - 1 };
    default:
      return state;
  }
};

// Create a Redux store
const store = createStore(reducer);

// Parent component
const Parent = () => {
  return (
    <Provider store={store}>
      <Child />
    </Provider>
  );
};

// Child component
const Child = () => {
  const count = useSelector(state => state.count);
  const dispatch = useDispatch();

  const handleIncrement = () => {
    dispatch(increment());
  };

  const handleDecrement = () => {
    dispatch(decrement());
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleIncrement}>Increment</button>
      <button onClick={handleDecrement}>Decrement</button>
    </div>
  );
};

export default Parent;
Enter fullscreen mode Exit fullscreen mode

In this example, we start by defining an initial state object and two actions (increment and decrement). We then define a reducer function that handles the state updates based on the dispatched actions.

We create a Redux store using the createStore function from Redux and pass the reducer to it.

In the parent component, we wrap the Child component with the Provider component from react-redux and pass the Redux store as a prop.

In the Child component, we use the useSelector hook to access the count state from the Redux store. We also use the useDispatch hook to get a reference to the dispatch function. When the increment or decrement buttons are clicked, we dispatch the corresponding action using the dispatch function.

Leveraging React Query for Server-State Management

For scenarios involving server-side state management, such as data fetching and caching, we introduce React Query.

import React from 'react';
import { useQuery, useMutation, QueryClient, QueryClientProvider } from 'react-query';

const queryClient = new QueryClient();

const fetchData = async () => {
  const response = await fetch('https://api.example.com/data');
  const data = await response.json();
  return data;
};

const postData = async (newData) => {
  const response = await fetch('https://api.example.com/data', {
    method: 'POST',
    body: JSON.stringify(newData),
    headers: {
      'Content-Type': 'application/json',
    },
  });
  const data = await response.json();
  return data;
};

const DataComponent = () => {
  const { data, isLoading, isError } = useQuery('data', fetchData);
  const mutation = useMutation(postData);

  const handleSubmit = async () => {
    await mutation.mutateAsync({ name: 'John Doe' });
  };

  if (isLoading) {
    return <p>Loading...</p>;
  }

  if (isError) {
    return <p>Error loading data</p>;
  }

  return (
    <div>
      <p>Data: {data}</p>
      <button onClick={handleSubmit}>Submit</button>
    </div>
  );
};

const Parent = () => {
  return (
    <QueryClientProvider client={queryClient}>
      <DataComponent />
    </QueryClientProvider>
  );
};

export default Parent;
Enter fullscreen mode Exit fullscreen mode

In this example, we start by creating a QueryClient instance and a fetchData function that fetches data from a server endpoint.

We define a postData function that makes a POST request to save new data on the server.

In the DataComponent, we use the useQuery hook to fetch data using the fetchData function. It returns an object with properties like data, isLoading, and isError to handle loading and error states.

We use the useMutation hook to handle the POST request using the postData function. The mutation object returned by useMutation includes a mutateAsync method that can be called to trigger the mutation.

Inside the handleSubmit function, we call mutation.mutateAsync to send a new data object to the server.

We render the data and a submit button. While loading, we display a loading message, and if there's an error, we display an error message.

The Parent component wraps the DataComponent with the QueryClientProvider to provide the QueryClient instance to the components that use React Query.

With this setup, React Query handles the management of server-state, caching, and data fetching, making it easier to track, update, and display server data in a React component.

Conclusion

React state management provides a spectrum of options, ranging from the simplicity of useState() and the Context API to more sophisticated libraries like Redux. While it's tempting to reach for the big guns like Redux, it's important to evaluate the needs of your application. Simple solutions like the Context API can often suffice for smaller projects and avoid unnecessary complexity.

By understanding the strengths and trade-offs of different state management approaches, you can make informed decisions when choosing the right solution. Remember, it's not always necessary to introduce heavyweight frameworks when simpler alternatives can fulfill your requirements effectively. Embrace the flexibility of React's state management ecosystem and select the approach that best aligns with the size and complexity of your project.

Happy coding! 😄👍

Top comments (3)

Collapse
 
explorykod profile image
Amaury Franssen

Thanks for this article, useful.

Collapse
 
codezera profile image
Bhavesh Yadav

Most welcome brother

Collapse
 
androaddict profile image
androaddict

Nice intro thanks