renatorib / react-powerplug

:electric_plug: Renderless Containers
https://renatorib.github.io/react-powerplug
MIT License
2.68k stars 101 forks source link

Add resetState, reset to powerplug #148

Closed istarkov closed 6 years ago

istarkov commented 6 years ago

Ref #110

see comment why

I've added in first commit eslint flow plugin as otherwise it's hard to edit flow file in editors like vscode (flow file is one big eslint error)

cc @TrySound please check and i'll proceed with other Components

istarkov commented 6 years ago

The possible issues with current reset implementation is that if object Shape will be changed in initial, setState inside reset will merge new Shape with previous instead reset.

To avoid we need to make instead of this

class State extends Component {
  state = {
    ...this.props.initial,
  }

this

class State extends Component {
  state = {
    value: { ...this.props.initial },
  }

It's fun but it was the first PR I made to react-redux ;-) moving state into value

istarkov commented 6 years ago

Fixed in above setState implementation

istarkov commented 6 years ago

BTW for my own case, I don't need that reset for every helper, and if we add Prop types to setState at State helper updater, I can reset State without resetState method, just using

setState((_, {initial}) => initial)

So feel free to close the PR, as it so-so needed ;-) and for my case (having that I have stable object shapes) result can be achieved just making few flow type changes

istarkov commented 6 years ago

Close in favor of #151

PS: TODO: Pick flow eslint from this PR and create new