npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dreamworld/addon-redux

v1.1.0

Published

Storybook addon for Redux Components

Downloads

2,775

Readme

Redux Addon

addon-redux is a Storybook addon that helps when building stories using components that use redux state.

Ideally stories are only needed for non-redux connected components, not containers. However, when writing stories for components of a redux application, it is common for the components to have conatiners as children which causes problems. This is where addon-redux helps out by providing a decorator and helpful panels to support container components.

This documentation is for version 2, click here for information on setting up version 1.

Redux Addon State Panel

Demo project using the Redux Addon.

This addon is compatible with Storybook for React

Features

  • Add two panels to Storybook: Redux State and Redux History
  • Wraps stories with a React Redux Provider component
  • View and Edit the current state of the store
  • Resets redux to initial state when switching stories
  • Provides a story parameter to update the store on story load
  • Logs actions and maintains the time, action, previous state, next state and state diff
  • Supports time travel to previous states

Install

npm install addon-redux

Usage

In order for the React Redux addon to function correctly:

  • it must be registered as a Storybook addon
  • its store enhancer must be used in the app's store
  • the store must be imported in preview.js

Register

Similar to other Storybook addons, the Redux Addon needs to be registered with storybook before use.

// .storybook/main.js
module.exports = {
  stories: [
    "../src/**/*.stories.mdx",
    "../src/**/*.stories.@(js|jsx|ts|tsx)"
  ],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@storybook/preset-create-react-app",
    "addon-redux"
  ]
}

Enhancer

To give the Redux Addon the ability to listen to and alter the store, its enhancer must be used when creating the store as shown below.

// Simplest use of the Redux Addon Enhancer
import { createStore, compose } from 'redux'
import reducer from './your/reducer'
import { enhancer } from 'addon-redux'

export const store = createStore(reducer, {}, enhancer)

Usually the store of an application will already be using enhancers. A more realistic store setup for the Redux Addon is shown below. It includes the commonly used middleware enhancer along with some middlewares for demonstration. This example shows how the Redux enhancer can be used with other enhancers, although the store creation code can look very different between different applications.

// Realistic use of the Redux Addon Enhancer with other store enhancers
import { createStore, compose, applyMiddleware } from 'redux'
import { enhancer as withReduxEnhancer } from 'addon-redux'
import reducer from './your/reducer'
import createMiddlewareEnhancer from './middlewareEnhancer'
import invariant from 'redux-immutable-state-invariant'
import logger from 'redux-logger'

createMiddlewareEnhancer () => {
  const middleware = []
  if (process.env.NODE_ENV !== 'production') {
    // include other middlewares as needed, like the invariant and logger middlewares
    middleware.push(invariant())
    middleware.push(logger())
  }
  return applyMiddleware(...middleware)
}

const createEnhancer = () => {
  const enhancers = []
  enhancers.push(createMiddlewareEnhancer())
  if (process.env.NODE_ENV !== 'production') {
    enhancers.push(withReduxEnhancer)
  }
  return compose(...enhancers)
}

const store = createStore(reducer, createEnhancer())

export default store

Import the Store (Preview.js)

The store must be imported in ./storybook/preivew.js so that it will be setup and ready for the stories. This addon will automatically wrap stories with the Redux provider as long as the enhancer has been setup as shown above.

This can be done in two ways:

  1. Import the store at the top of your file synchronously
// .storybook/preview.js

const store = require('./your/store')

module.exports = {
  decorators: []
}
  1. Import the store asynchronously using Storybook loaders
// .storybook/preview.js

module.exports = {
  decorators: [],
  loaders: [
    async () => ({
      store: await import('../stories/store'),
    })
  ]
};

Redux Addon History Panel

Args

Further control of the redux state is provided using storybook args. Args can be linked to the redux store using the ARG_REDUX_PATH key in the argTypes key of the default CSF export. The value of the ARG_REDUX_PATH is a dot delimited string representing the path that the arg corresponds to in the store. Integer segments are treated as array indices.

import React from 'react'
import App from './App'
import { ARG_REDUX_PATH } from 'addon-redux'

export default {
  title: 'App',
  component: App,
  argTypes: {
    name1: {
      control: { type: 'text' },
      [ARG_REDUX_PATH]: 'todos.0.text'
    }
  }
};

const Template = (args) => <App />;

export const All = Template.bind({});
All.args = {
  name1: 'First Value',
  completed1: false
};

Parameters

addon-redux currently supports one storybook parameter that can be used to change the redux state on story load, PARAM_REDUX_MERGE_STATE. This parameter takes a JSON string or object that will be parsed and spread on top of the current store's state.

// example story using PARAM_REDUX_MERGE_STATE
import React from 'react'
import MyComponent from './MyComponent'
import { PARAM_REDUX_MERGE_STATE } from 'addon-redux'

export default {
  title: 'MyComponent',
  component: MyComponent,
  parameters: {
    [PARAM_REDUX_MERGE_STATE]: '{"foo": {"bar": "baz"}}'
  }
};

const Template = (args) => <MyComponent {...args} />;

export const All = Template.bind({});
All.args = {};