React setting state using …prevState

本秂侑毒 提交于 2020-12-15 06:37:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!