DEV Community

Discussion on: React with Typescript

kylesureline profile image
Kyle Scheuerlein

Yeah, an object is probably not ideal for useState, but you can optimize it by passing a function. Instead of doing this...

setInput({...input, [name]: value}) can do this:

setInput((prevState) => ({...prevState, [name]: value}))

The key distinction is that with the first approach, handleChange really ought to be a useCallback with input as a dependency, but with the function approach it is not necessary.

I’m not sure that several useStates is better, actually. How about a useReducer instead?