Reputation: 85
I have a method that set a state variable using the setState. How can I convert this method using React hooks. Thanks
Upvotes: 2
Views: 2700
Reputation: 683
Instead of State it will become:
const [state, setState] = useState({});
you can invoke setState like this:
setState({ [e.target.name]: e.target.value });
Upvotes: 4
Reputation: 20007
It’s almost the same.
const [state, setState] = useState({});
// …
setState(() => ({ [e.target.name]: e.target.value }));
Upvotes: 0