New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

storybook-react-context

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

storybook-react-context

Manipulate React context inside Storybook. Read state and dispatch updates from outside of React component.

  • 0.5.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
24K
decreased by-5.24%
Maintainers
1
Weekly downloads
 
Created
Source

storybook-react-context

Manipulate React context inside Storybook. Read state and dispatch updates from outside of React component.

React examples

Install

npm install -D storybook-react-context

Usage

Add withReactContext decorator where needed, per component or globally.

import { withReactContext } from 'storybook-react-context';

export default {
  title: 'some story',
  decorators: [withReactContext],
};

Options

withReactContext takes an argument which is an object with the following optional properties:

  • context - custom context returned by React.createContext
  • reducer - custom reducer (defaults to a simple assignment of dispatch action on the current state)
  • 'useReducer' - when set to false avoids using useReducer hook in provider value, instead pass the initialState directly
  • initialState - initial state to use in useReducer for context provider value

Initial context state can also be set in parameters using initialState key:

someComponent.parameters = {
  initialState: {
    defaultValue: true,
  },
};

When both initialState values (in decorator argument and parameters) are objects they are combined (assigned), otherwise the either initialState in parameters or decorator argument will be used (in that order).

Component will be wrapped with another component which uses the context hook and returns it to the story via story context as the result of React.useReducer with reducer function and initialState.

import { withReactContext } from 'storybook-react-context';

export const myStory = (_, { context: [state, dispatch] }) => (
  <button onClick={() => dispatch({ text: 'Changed' })}>{state.text}</button>
);
myStory.decorators = [withReactContext({
  initialState: {
    title: 'Initial #1'
  }
})];
myStory.parameters.initialState = {
  initialState: {
    text: 'Initial #2',
  },
};

Keywords

FAQs

Package last updated on 22 Jan 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc