MicheleBertoli / react-automata

A state machine abstraction for React
MIT License
1.34k stars 60 forks source link

Bug: Component gets rerendered when trying to transition to impossible state #61

Closed kirillku closed 6 years ago

kirillku commented 6 years ago

First of all, thanks for a great lib. I started to use react-automata in my project and found one issue. When there is an transition to impossible state, the transition doesn't happen, but component still gets rerendered. As for me, in this case nothing should happen. I checked the code, looks like you do setState on wrapper component even if a transition was not possible and this results in rerendering wrapped component.

Here is a codesandbox with example: https://codesandbox.io/s/9lz8jq68ro

MicheleBertoli commented 6 years ago

Thank you very much for opening this issue, @kirillku.

I confirm that, at the moment, react-automata is setting the state on each transition call (even when the machine state is not changed). I'm planning to implement a few optimisations soon.

However, this shouldn't be an issue as your components should be able to render multiple times with no side-effects. In any case, if you want to prevent the component to re-render when the state doesn't change, you can extends PureComponent (instead of Component).

I hope this helps.

MicheleBertoli commented 6 years ago

Hey @kirillku, v4 is out and now the component renders only when the machine state actually changes.

screen shot 2018-08-09 at 3 35 52 pm