# @bluealba/with-state

> Wrapper around @dump247/storybook-state to be used as an storybook decorator

Latest version **1.0.1** (published 2018-11-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @bluealba/with-state
pnpm add @bluealba/with-state
yarn add @bluealba/with-state
bun add @bluealba/with-state
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-11-28 |
| First published | 2018-11-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Claudio Fernandez |
| Maintainers | aitrusgit, jabsatz, pablote, gunthers |

## Links

- npm: https://www.npmjs.com/package/@bluealba/with-state
- Repository: https://github.com/bluealba/withState
- Homepage: https://github.com/bluealba/withState#readme
- Issues: https://github.com/bluealba/withState/issues
- npm.io page: https://npm.io/package/@bluealba/with-state

## Recent versions

- 1.0.1 (latest) — 2018-11-28

## README

@bluealba/withState
===
This is a wrapper around [@dump247/storybook-state](https://github.com/dump247/storybook-state) designed to be used as an storybook decorator. This is a VERY small and rather hacky util.

## Install
You can install it with `npm` using

```
npm install @bluealba/withState
```

## Usage

Just add `withState` decorator using storybook's `addDecorator` function

```javascript
import React from "react";
import { storiesOf } from "@storybook/react";
import { withState } from "@bluealba/withState";
import { Checkbox } from "./Checkbox";

storiesOf("Checkbox", module)

	.addDecorator(withState({ isSelected: false }))

	.add("An ordinary unmanaged component", ({store}) => (
		<Checkbox
			className="checkbox size-check"
			isSelected={store.state.isSelected}
			onChange={() => {
				store.set({ isSelected: !store.state.isSelected });
			}}
		/>
	))
```

`withState` function receives as very first parameter the initial state that will be used for all the stories that don't
provide an initial state.

### Specifying the intial state

Stories can overwrite the `initialState` value through their story parameters


`withState` function receives as very first parameter the initial state that will be used for any story that doesn't 
specify it. Stories can overwrite such value through their parameters:

```javascript
storiesOf("Checkbox", module)

	.addDecorator(withState({ isSelected: false }))

	.add("An ordinary unmanaged component, checked by default", ({store}) => (
		<Checkbox
			className="checkbox size-check"
			isSelected={store.state.isSelected}
			onChange={() => {
				store.set({ isSelected: !store.state.isSelected });
			}}
		/>
	), { initialState: { isSelected: true } })
```

### Combining with other decorators

Since original `@dump247/storybook-state` HOC object is added later we are now able to combine this decorator with others that rely on component introspection. In other words, this now works perfectly:

```javascript
import React from "react";
import { storiesOf } from "@storybook/react";
import { withState } from "@bluealba/withState";
import { withInfo } from "@storybook/addon-info";
import { Checkbox } from "./Checkbox";

storiesOf("Checkbox", module)

	.addDecorator(withInfo(""))
	.addDecorator(withState({ isSelected: false }))

	.add("An ordinary unmanaged component", ({store}) => (
		<Checkbox
			className="checkbox size-check"
			isSelected={store.state.isSelected}
			onChange={() => {
				store.set({ isSelected: !store.state.isSelected });
			}}
		/>
	))
```

---
_Source: https://npm.io/package/@bluealba/with-state · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
