React Side-Effects
Explore how to use React's useEffect Hook to manage side-effects like syncing the search term with browser local storage. Understand dependency arrays and how to cleanly separate side-effect logic from state updates for robust React apps.
We'll cover the following...
We’ll make the Search component remember the most recent search interaction, so the application opens it in the browser whenever it restarts.
First, use the local storage of the browser to store the searchTerm accompanied by an identifier. Next, use the stored value, if a value exists, to set the initial state of the searchTerm. Otherwise, the initial state defaults to our initial state (here “React”) as before:
When using the input field and refreshing the browser tab, the browser should remember the latest search term. Using the local storage in React can be seen as a side-effect because we interact outside of React’s domain by using the browser’s API.
There is one flaw, though. The handler function should mostly be concerned about updating the state, but now it has a side-effect. If we use the setSearchTerm function elsewhere in our application, we will break the feature we implemented because we can’t be sure the local storage will also get updated.
To fix this, we handle the side-effect at a dedicated place using React’s useEffect Hook. This hook is essential for managing the React component lifecycle in functional components.
React’s useEffect Hook takes two arguments:
- The first argument is a function where the side-effect occurs. In our case, the side-effect is when the user types the
searchTerminto the browser’s local storage. - The second argument is a dependency array of variables. If one variable changes, the function for the side-effect is called. In our case, the function is called every time the
searchTermchanges; it’s called initially when the component renders for the first time.
If the dependency array of React’s useEffect is an empty array, the function for the side-effect is only called once, after the component renders for the first time. The hook lets us opt into React’s component lifecycle. It can be triggered when the component is first mounted, but also one of its dependencies are updated.
Using React useEffect instead of managing the side-effect in the handler has made the application more robust. Whenever and wherever searchTerm is updated via setSearchTerm, local storage will always be in sync with it.
📜 Check the persistence by opening the app link in a tab, changing the search term, closing the tab, reopening the app link. Note that the session is live for only 30 minutes. After this the server restarts.