javascript - How to use Variables in setState in JSX -
i use variable in setstate function in jsx file react.
how restructure code:
var name = e.target.name; if(name == "title"){ this.setstate({ title: e.target.value}); } else if(name == "date"){ this.setstate({ date: e.target.value}); } else if(name == "amount"){ this.setstate({ amount: e.target.value}); }
into (so don't repeat myself)?
var name = e.target.name; this.setstate({ name: e.target.value});
the above syntax sets state of "name" , not value of "name" variable.
easy:
var newstate = {}; newstate[e.target.name] = e.target.value; this.setstate(newstate);
Comments
Post a Comment