我有一个状态,它是一个对象。当我更改对象的属性并更新状态时,它不会重新呈现,但是当我创建一个与旧值合并的对象的新实例时,它的工作原理便会发生。为什么会发生?
const [client, setClient] = useState({
name: "",
company_name: "",
email: "",
cpf: "",
cnpj: "",
type_person: 1,
telephone: "",
});
const onChangeClient = (e) => {
client[e.target.name] = e.target.value;
setClient(client); // not works
setClient(prevClient => ({...prevClient, ...client})); // works
}