I am using storybook
(this) to play with my components in isolation. I want to mock all the flux cycle (that in the full app it is done with the help of redux
) and update a property using a simple object in the story, but I am missing something.
storiesOf('Color picker', module).add('base', () => {
let colorPickerState = {
changeColor: function(data) {
this.color = data.color
},
color: '#00aced'
}
return (
<ColorPicker
name="color"
onChange={colorPickerState.changeColor.bind(colorPickerState)}
value={colorPickerState.color}
/>
)
}
I expect the value
prop of <ColorPicker />
to be updated when the onChange
is called; I can see the value of colorPickerState.color
being updated correctly, but the component does not re-render.
What am I missing?
I would try employing the
useState
hook from react - updating state values via its setters seems to have the effect of re-rendering your storybook component.I had a similar problem where my data input form's state and event handling (including complex validation) is done in globally, up a level in the component tree. I was able to get a working demo of this component and its complex validation by writing a simple event handler to pass along to my component.
Do note that my code is in Typescript. I am pretty new to the React world so this might not be perfect.