简体   繁体   English

使用 Redux 在 React Hooks 应用程序中添加 Storybook 旋钮

[英]Adding Storybook knobs in React Hooks app with Redux

I have an app using React hooks, and I am also using there Storybook.我有一个使用 React 钩子的应用程序,我也在那里使用 Storybook。 After adding Redux I have a small problem.添加 Redux 后,我遇到了一个小问题。 While adding Provider as global decorator helped me load the initial state for my components and display them in Storybook, now that everythign is in store, I cannot figure out how to add knobs to be able to display my components with different state.虽然将 Provider 添加为全局装饰器帮助我加载组件的初始状态并将它们显示在 Storybook 中,但现在所有内容都已存储,我无法弄清楚如何添加旋钮以能够以不同的状态显示我的组件。

Here's the config file with Provider这是 Provider 的配置文件

const store = createStore(reducer);  
addDecorator(Story => (<div
style={style}>
<Provider store={store}>
<Story />
</Provider>   </div> ));

And here's one of the stories which worked fine before Redux (with useSelector and useDispatch hooks) was added这是在添加 Redux(带有 useSelector 和 useDispatch 钩子)之前运行良好的故事之一

const stories = storiesOf('UI Components', module)
stories.addDecorator(withKnobs)
stories.add('Input Button', () => {
const groupId = 'Props'
const statusesKnob = select('Button states', appStates, Object.values(appStates)[0], groupId)
  return <InputButton getAppState={statusesKnob} />
})

States状态

const appStates = [
  LOADING: 'LOADING',
  INACTIVE: 'INACTIVE',
  ERROR:'ERROR',
]

Thanks谢谢

I had the same problem.我有同样的问题。 I finally added the store to the a global variable and used store.dispatch我终于将商店添加到全局变量并使用了 store.dispatch

if decorator.js: window.REDUX_STORE = store;如果decorator.js: window.REDUX_STORE = store;

in the story: window.REDUX_STORE.dispatch(...);在故事中:window.REDUX_STORE.dispatch(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM