Sync React application state with the URL

A simplified approach to managing external states

Wednesday, September 21, 2022

sink

TL;DR


Introduction

Wouldn't it be great if we can allow our application to store its state, and we can easily share it with others? For instance, on the GIF below, we can sort a table by the different properties; then, the URL will update accordingly. And the next time the same URL is loaded, the user will see the same view.


GIF

Of course, it could be more complex; instead of a table, use a card view filtered by the multiple properties and sorted by age in descending order, etc.

This idea is pretty common, but the question is, how do we implement it?


What do we need to implement the feature?

Below are typical high-level steps to develop the requirement

  1. Create an App component to store and load data
  2. Create the Table component
  3. Have a place to store the sort state
  4. Handle updating the URL when a table header is clicked
  5. Fetch the URL state, then update the component

Steps 1 & 2 are probably the easiest, as they are our average react components.

Step 1: Create the plain version of the App that store and load data


Step 2: Create a simple table component


Complicated solution

Before going to the straightforward solution, let's see another way a React developer might do it.


Step 3: Store the sortBy state using the React.useState hook

Instead of putting sortBy in the Table component, we lift the state from the table to the parent component to present data differently(i.e. Card View)

Step 4.1: Add table headers event handler

Add an event listener when a table header is clicked; then, propagate the event to the parent component.

Step 4.2: Saving the React-related state to the URL

Saving the state to the URL is just as simple as changing the URL itself. The way to change the URL depends on your routing library of choice. For the example below, I used the classic react-router.

Step 5: Loading State from URL

Before rendering our component, we retrieve the state of the URL and update the value of our sortBy state.

Now, it should be working as expected!

GIF

But what if we have more queries to process?

If we follow the previous approach, our code might look like this.

You might say that we can just use a useReducer or some state management, but it does not address the root of the issue.


What is the issue were are trying to solve?

The problem is creating two sources of truth: our React state and the URL state. The worse part is if we update those states separately. If possible, follow the DRY principle.


Simplified solution

Step 3: Use the actual URL object as the single source of truth

There is no need to create an additional state that we can track and modify. Simply derive the queries from the URL, and store the value in something immutable.

Step 4(Simplified): Update the URL directly

Our table component does not need to propagate state changes to the parent component. By updating the URL directly, we can effectively revise our single source of truth.

Since we just need to update the URL to update the state, we can even leverage the default behavior of HTML to update our state.

Final Solution

More about the single source of truth thingy

If we want to create a custom table header component, we don't need to deal with prop drilling because our single source of truth is in the URL itself.

It's like we use the URL as Redux.

It just looks amazing how we could eliminate prop drilling.

After applying the additional changes, we're back to our desired behavior

GIF

This pattern can also be used for other states, such as app storage and server state(react-query); also with other frameworks.

Conclusion

It's easy to miss a more straightforward solution because of the pattern that we are used to. The point here is not about avoiding the usage useState, useReducer, redux, etc. It is more on utilizing the web's default behavior to reduce our code's complexity.