Home > other >  how to make construction this.setState({ [name]: value }) with useState hook
how to make construction this.setState({ [name]: value }) with useState hook

Time:05-16

I have a function with code:

const { name, value, } = e.currentTarget;
this.setState({ [name]: value })

How can I make this construction with useState hook?

CodePudding user response:

It's unpossible with useState since its modyfing function is stored in variable.

CodePudding user response:

const [state, setState] = useState(initialState);
const { name, value, } = e.currentTarget;
const values = {[name]: value}
setState(prevState => {
  return {...prevState, ...values};
});
  • Related