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

Viewed 37

I have a function with code:

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

How can I make this construction with useState hook?

2 Answers

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

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