dump247 / storybook-state

Manage component state in React storybook.
MIT License
76 stars 17 forks source link

State is not properly updated on call in componentDidMount/constructor #28

Open kevich opened 4 years ago

kevich commented 4 years ago

For now, when we wrap component with withStore and wrapped component uses update state function inside componentDidMount method or constructor - the state wont be updated properly.

The demo can be found here:

http://kevich.github.io/issueDemos/dump247-storybook-state-update-issue-demo/storybook-static/?path=/story/storetest--test-with-did-mount

And code for demo component here:

https://github.com/kevich/kevich.github.io/blob/master/issueDemos/dump247-storybook-state-update-issue-demo/src/StoreTest.jsx

As of the reason of this behaviour is that subscribe function of Store in plugin called at componentDidMount of StoryState wrapper (https://github.com/dump247/storybook-state/blob/master/src/index.js#L63).

But componentDidMount method or constructor of Component is called earlier than method componentDidMount of StoryState wrapper, because StoryState is parent for Component.

Maybe the solution will be to move store.subscribe(this.handleStateChange); to constructor of StoryState class?