Socket
Socket
Sign inDemoInstall

storybook-react-context

Package Overview
Dependencies
58
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

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.


Version published
Weekly downloads
16K
decreased by-9.3%
Maintainers
1
Install size
5.98 MB
Created
Weekly downloads
 

Readme

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],
};

The decorator can also be preconfigured for all stories in the module:

export default {
  title: 'some story',
  decorators: [
    withReactContext({
      Context: ExampleContext,
      initialState: { authenticated: false },
    }),
  ],
};

Options

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

  • Context - The context returned by React.createContext to provide for story's components
  • reducer(state, action) - custom reducer to produce the provider value or;
  • useProviderValue(state, args) - a function (hook) to be used to derive the provider value (provides story args as second argument to link with Storybook Controls)
  • initialState - the initial state to use for the provider value

The decorator options can also be se set in story parameters using reactContext key:

someComponent.parameters = {
  reactContext: {
    initialState: {
      defaultValue: true,
    },
    reducer: (state, action) => ({ ...state, action })
  }
};

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.

The useProviderValue hook can be used to link the context with Storybook Controls.

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

export const myStory = ({ myValue }) => (
  <button onClick={() => dispatch({ text: 'Changed' })}>{state.text}</button>
);
myStory.args = {
  myValue: true,
}
myStory.decorators = [withReactContext];
myStory.parameters.reactContextState = {
  useProviderValue: (state, args) => args
};

Keywords

FAQs

Last updated on 21 Feb 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc