DEV Community

loading...

Discussion on: Fast & easy... React states management in one function

Collapse
kayis profile image
K

Yeah, curried functions are perfect for this. The arrow syntax lends itself to this.

valueChange = (key) => {
  return function (e) {
    var obj= {};
    state[key] = e.target.value; //<-- this is a bug btw.
    this.setState(obj);
  }.bind(this);
}

becomes

valueChange = key => e => this.setState(oldState => ({
  ...oldState,
  [key]: e.target.value
}))
Collapse
patroza profile image
Patrick Roza

Or just valueChange = key => e => this.setState({[key]: e.target.value})

Collapse
alephnaught2tog profile image
M. Shemayev

Yesssssssssssssss

Collapse
genta profile image