DEV Community

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

Collapse
 
kayis profile image
K • Edited

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);
}
Enter fullscreen mode Exit fullscreen mode

becomes

valueChange = key => e => this.setState(oldState => ({
  ...oldState,
  [key]: e.target.value
}))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
alephnaught2tog profile image
Max Cerrina

Yesssssssssssssss

Collapse
 
patroza profile image
Patrick Roza

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

Collapse
 
genta profile image
Fabio Russo

Love It!