【发布时间】:2020-10-12 09:51:18
【问题描述】:
我有以下代码...
const Thing = ({...})=> {
const initialState = {
foo: ''
}
const [state, setState] = useState(initialState);
const changeFormvalue = (e) => {
state.foo = e.target.value;
setState(state);
}
return (
<input type="text" name ="foo"
value={state.foo}
onChange={changeFormvalue} />
)
}
当我运行时,我看到它命中了函数,我看到它设置了值。但是,在 setState 命令之后,页面并没有重新渲染,值也没有更新。
为什么页面没有更新?
【问题讨论】:
-
因为您直接改变状态并传递
setState相同的对象引用。 React 不会看到更新。
标签: reactjs react-functional-component