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.
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
Create an App component to store and load data
Create the Table component
Have a place to store the sort state
Handle updating the URL when a table header is clicked
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
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)
App.jsx
123456789101112131415161718192021constApp=()=>{const[sortBy, setSortBy]=React.useState('');// Create a sortedData based on the value of `sortProp`const sortedData = sortBy?[...data].sort((a, b)=>{const firstValue = a[sortBy];const secondValue = b[sortBy];return firstValue && secondValue? firstValue.localeCompare(secondValue):0;}): data;return(<div><Tabledata={sortedData}sortBy={sortBy}setSortBy={setSortBy}/></div>);}
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.
components/Table.jsx
123456789101112131415161718192021222324252627import{ useNavigate, createSearchParams }from'react-router-dom';constTable=({data, setSortBy, sortBy})=>{// react-outer utility hook to manage navigationconst navigate =useNavigate();consthandleSorting=(value)=>{// Check if the new value is the active sortBy valueconst isActiveSort = value === sortBy;// Update sortBy in the React landsetSortBy(isActiveSort ?'': value);// Update sortBy in the URL landif(isActiveSort){navigate({});}else{navigate({search:`?${createSearchParams({sortBy: value,})}`,});}};// return ...}
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.
App.jsx
12345678910111213141516import{useSearchParams}from'react-router-dom';constApp=()=>{const[sortBy, setSortBy]=React.useState('');// React router utility hook to get the query paramsconst[searchParams]=useSearchParams();useEffect(()=>{const sortByQuery = searchParams.get('sortBy');setSortBy(sortByQuery);},[searchParams]);// return ...}
Now, it should be working as expected!
But what if we have more queries to process?
If we follow the previous approach, our code might look like this.
App.jsx
12345678910111213141516171819202122232425262728293031323334353637constApp=()=>{const[sortBy, setSortBy]=React.useState('');const[sortByOrder, setSortByOrder]=React.useState('');const[filterBy, setFilterBy]=React.useState('');const[view, setView]=React.useState('table');const[focusedRow, setFocusedRow]=React.useState(-1)const[searchParams]=useSearchParams();useEffect(()=>{const sortByQuery = searchParams.get('sortBy');const filterByQuery = searchParams.get('filterBy');const viewQuery = searchParams.get('view');const focusedRowQuery = searchParams.get('focusedRow');setSortBy(/* Derive sort field from sortByQuery */);setSortByOrder(/* Derive sort order from sortByQuery */);setFilterBy(filterByQuery);setView(viewQuery);setFocusedRow(focusedRowQuery);},[searchParams]);const sortedData =// process sorted datareturn(<div><Tabledata={sortedData}sortBy={sortBy}setSortBy={setSortBy}sortByOrder={sortByOrder}setSortByOrder={setSortByOrder}filterBy={filterBy}setFilterBy={setFilterBy}view={view}setView={setView}focusedRow={focusedRow}setFocusedRow={setFocusedRow}/></div>);}
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.
App.jsx
12345678910111213141516constApp=()=>{// const [sortBy, setSortBy] = React.useState('');// const [searchParams] = useSearchParams();// useEffect(() => {// const sortByQuery = searchParams.get('sortBy');// setSortBy(sortByQuery);// }, [searchParams]);// This is the simplified version of Step 5const[searchParams]=useSearchParams();const sortBy = searchParams.get('sortBy')||'';// ...}
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.
components/Table.jsx
1234567891011121314151617181920212223242526272829303132333435// const Table = ({data, setSortBy, sortBy}) => {constTable=({data})=>{const[searchParams]=useSearchParams();const sortBy = searchParams.get('sortBy');consthandleSorting=(value)=>{// Check if the new value is the active sortBy valueconst isActiveSort = value === sortBy;// setSortBy(isActiveSort ? '' : value);if(isActiveSort){navigate({});}else{navigate({search:`?${createSearchParams({sortBy: value,})}`,});}};return(<table><thead><tr><thonClick={()=>handleSorting('username')}>Username</th><thonClick={()=>handleSorting('name')}>Name</th><thonClick={()=>handleSorting('website')}>website</th></tr></thead>{/* ... */}</table>)}
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.
components/Table.jsx
123456789101112131415161718192021222324252627282930import{Link, useSearchParams}from'react-router-dom';constTable=({data})=>{const[searchParams]=useSearchParams();const sortBy = searchParams.get('sortBy');// To avoid repetition in the `to` attributeconstcreateToLink=(sortValue)=>{return sortBy === sortValue ?'/':`?sortBy=${sortValue}`;};return(<table><thead><tr><th><Linkto={createToLink('username')}>Username</Link></th><th><Linkto={createToLink('name')}>Name</Link></th><th><Linkto={createToLink('website')}>Website</Link></th></tr></thead>{/* ... */}</table>)}
Final Solution
App.jsx
1234567891011121314151617181920212223242526272829303132333435363738394041424344importReact,{useEffect}from'react';import{useSearchParams}from'react-router-dom';importTablefrom'./Table';constApp=()=>{const[data, setData]=React.useState([]);useEffect(()=>{fetch('https://jsonplaceholder.typicode.com/users').then(response=> response.json()).then(json=>setData(json));},[]);// Implementing full sorting behavior is not the focus of this blog// That is why the following code magically appears hereconst[searchParams]=useSearchParams();const sortBy = searchParams.get('sortBy')||'';const[sortProp, order]= sortBy ? sortBy.split(':'):[];const sortedData = sortBy?[...data].sort((a, b)=>{const firstValue = a[sortProp];const secondValue = b[sortProp];if(!firstValue ||!secondValue){return0;}return order ==='desc'? secondValue.localeCompare(firstValue): firstValue.localeCompare(secondValue);}): data;return(<div><Tabledata={sortedData}/></div>);};exportdefaultApp;
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.
After applying the additional changes, we're back to our desired behavior
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.