Huge News!Announcing our $40M Series B led by Abstract Ventures.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.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27K
increased by22.48%
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 keys:

  • context - custom context returned by React.createContext
  • reducer - custom reducer (defaults to a simple assignment of dispatch action on the current state)
  • 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 12 Jul 2021

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