react-side-effect
Create components whose nested prop changes map to a global side effect
A React utility that collects props from nested components across your whole app to trigger a single combined side effect, like updating the page title.
Understanding React Side Effect
React Side Effect is a utility library that lets you create React components that automatically trigger actions whenever their props change—and it does this in a smart way by collecting information from multiple nested components before acting on it.
Here's a concrete example: imagine you're building a website where different pages or sections need to change the browser tab title. Instead of writing logic in each page component, you could wrap your content in a DocumentTitle component that accepts a title prop. Every time that prop changes, the library automatically updates document.title for you. If you have multiple DocumentTitle components nested inside each other, the library merges their settings intelligently, letting the deepest component win—so a specific page's title overrides a generic section title.
The key insight is that this library collects props from *all instances* of a component throughout your entire component tree, then runs a single function to combine them and decide what side effect to perform. This is different from the typical React approach where each component handles its own effects independently. It's useful for things like changing the page background color based on the current screen, triggering data-fetching actions declaratively, or managing document metadata (like the page title or social media tags).
The library includes built-in support for server-side rendering, which matters if you're building a Node.js backend that renders React to HTML. You can capture the aggregated state before sending the HTML to the browser, ensuring the right metadata ends up in the initial page. It also provides testing utilities so you can verify your side effects work as expected without actually triggering them.
Where it fits
- Wrap page sections in a DocumentTitle component so nested pages can override the browser tab title.
- Manage document metadata like social media tags declaratively from anywhere in the component tree.
- Capture aggregated side-effect state during server-side rendering so the initial HTML has correct metadata.
- Test that your app's side effects (like title changes) work correctly using the built-in testing utilities.