Photo by Etienne Girardet on Unsplash

How to use React Router v6 with Storybook

Mock react-router inside your stories

Marco Martino
Published in
1 min readAug 9, 2022

--

When developing components with react-router, you probably want them to work also inside your Storybook stories.

Out of the box, Storybook has no support for react-router, and you’ll see your components generating errors when you try to run them into a story.

Luckily, there is an easy fix for it, and it’s called Storybook Addon React Router v6. To make it work, you just need to install and use it as a decorator to add the router support for all the stories.

How to install

To install the Addon

Install Storybook Addon React Router v6

To load it on Storybook

Add the addon to storybook

How to use it as a decorator

To make it work, the easiest way is to add the Addon as a decorator to your .storybook/preview.js.

This was my initial configuration

Initial configuration

First, we load the Addon component withRouter, then we add it as a decorator

Load the Addon as a decorator

Make sure to restart your Storybook and we are done!

Additional configuration

The Addon allows for additional configurations that you can add as a global default on .storybook/preview.js

Addon additional configuration

--

--

Marco Martino
Soulweb Academy

Software Engineer, Open Source enthusiast, Full-stack Developer, Drupal specialist, React Developer, GIS Developer, Data lover