Storybook: How can I change a property in a stateless component?

Created on 15 May 2016  路  15Comments  路  Source: storybookjs/storybook

I have a stateless component:
<Button fired={fired} onClick={() => setState('fired': true)} />
how can I change fired property when I click on the button?

discussion feature request

Most helpful comment

May be we need to have some kind of reactive variables where we can change values. How about this:

{ storiesOf, ReactiveVar } from '@kadira/storybook';

const fired = new ReactiveVar(false);

storiesOf('Button')
  .add('simple-button', () => {
    <Button fired={fired.get()} onClick={fired.set(!fired.get())} />
  })

Once you set the value, the related story will get re-rendered again.
This can be even built completely outside of @kadira/storybook if we expose an API to reload the story.

All 15 comments

Maybe wrap your button in a stateful component? eg:

const ButtonWrapper = React.createClass({
  render() {
     return <Button fired={fired} onClick={() => setState('fired': true)} />
  }
})

storiesOf('button', module)
  .add('', () => (<ButtonWrapper/>)

Yes, it will work. But it would be a good idea to solve this problem using Storybook API @arunoda.

@khmelevskii It's unlikely that we'll have a direct API for this, since stories needs to be static with it's data.

But, I think story linking might help you: https://github.com/kadirahq/react-storybook/blob/master/docs/writing_stories.md#linking-stories

@arunoda Story linking won't help me if, for example, I want to show animation when I click on button.

I have a similar problem where I have a chart and I want it to render different values while I am in the storybook, according to a control panel I put next to the chart component.

Being able to tell the storybook to re-render the story would solve the problem for me.

May be we need to have some kind of reactive variables where we can change values. How about this:

{ storiesOf, ReactiveVar } from '@kadira/storybook';

const fired = new ReactiveVar(false);

storiesOf('Button')
  .add('simple-button', () => {
    <Button fired={fired.get()} onClick={fired.set(!fired.get())} />
  })

Once you set the value, the related story will get re-rendered again.
This can be even built completely outside of @kadira/storybook if we expose an API to reload the story.

I think you should create your own wrapper component, that handles the state in this case.

I have a similar problem. My component fires an action when the user is in the middle of a mousemove event that should result in re-rendering the component with different props. I don't think linkTo is appropriate for this right?

I think exposing an API to re-render the story would be very helpful!

I think we do this via a wrapper component.

@arunoda Any idea why I can't import ReactiveVar for now?

@phthhieu I assume you are referring to Meteor's ReactiveVar.
If so, that's something coming from Meteor's package system. We don't support that.
We only support NPM.

@arunoda you used "ReactiveVar" as part of storybook package.

{ storiesOf, ReactiveVar } from '@kadira/storybook';

Is the ReactiveVar still available in storybook 3.x? If not what is it replaced with?

ReactiveVar was a proposed solution, not something which was implemented.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dnlsandiego picture dnlsandiego  路  3Comments

shilman picture shilman  路  3Comments

purplecones picture purplecones  路  3Comments

wahengchang picture wahengchang  路  3Comments

miljan-aleksic picture miljan-aleksic  路  3Comments