Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@dreamworld/addon-redux
Advanced tools
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.
Demo project using the Redux Addon.
This addon is compatible with Storybook for React
npm install addon-redux
In order for the React Redux addon to function correctly:
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"
]
}
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
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:
// .storybook/preview.js
const store = require('./your/store')
module.exports = {
decorators: []
}
// .storybook/preview.js
module.exports = {
decorators: [],
loaders: [
async () => ({
store: await import('../stories/store'),
})
]
};
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
};
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 = {};
FAQs
Storybook addon for Redux Components
We found that @dreamworld/addon-redux demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.