Please reset state properly when switching between states #33619
Replies: 2 comments 2 replies
-
|
Hi @binarykitchen! I'm Dosu and I’m helping the storybook team. Redux state persists across stories because Storybook does not automatically reset or recreate your Redux store when switching stories. This is a common scenario with global state libraries like Redux. The recommended solution is to use a Storybook decorator that wraps each story with a fresh Redux store instance, ensuring state isolation and reset between stories. This approach is documented in the official Storybook docs and is widely used in the community (docs, decorators guide, mocking providers). Here’s how you can do it: // .storybook/preview.js or preview.tsx
import React from 'react';
import { Provider } from 'react-redux';
import { configureStore } from '@reduxjs/toolkit';
import rootReducer from '../src/reducers'; // adjust to your root reducer
export const decorators = [
(Story, context) => {
// Optionally, use context.parameters to customize initial state per story
const store = configureStore({ reducer: rootReducer, preloadedState: context.parameters.initialState });
return (
<Provider store={store}>
<Story />
</Provider>
);
},
];You can now set Avoid using a single global store or only preview.js without a decorator, as this will not isolate state between stories. Workarounds like forcing a page reload or unmounting the root node exist, but the decorator pattern above is the best practice for Redux in Storybook (discussion, docs). You can try this pattern and see if it resolves your state reset issue. If you still see state leaking, please share your current decorator or provider setup for further review. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
I've figured it out with a storybook decorator. Thanks. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hello folks,
Wonderful library. React + Redux user here. I do notice, state isn't reset properly when switching between stories.
To reproduce,
git clonefrom https://github.com/binarykitchen/videomail-recorder.gitnpm installnpm run storybookI've already researched. There are some suggestions out there to use Storybook's preview or decorators to reset the state, which I feel not right. Because, it's all about states, and whenever you switch stories, all has to be reset properly. Even for React components.
Hence, raising this discussion here.
Additional information
No response
Create a reproduction
https://github.com/binarykitchen/videomail-recorder.git
Beta Was this translation helpful? Give feedback.
All reactions