问题
I have this state in my React application:
const [city, setCity] = useState({
name: "",
id: ""
});
I'm trying to change the state like so:
useEffect(() => {
setCity(prevState => ({...prevState, id: 'test'}))
}, [])
Doing this I get an error in WebStorm when hovering over the code:
Argument type function(any): {id: string} is not assignable to parameter type ((prevState: {name: string, id: string}) => {name: string, id: string}) | {name: string, id: string} ... Type string is not assignable to type undefined
Why do I get this error and how do I solve it?
来源:https://stackoverflow.com/questions/64879685/react-setting-state-using-prevstate