const [form, setForm] = useSafeState({
name: 'react-use',
description: 'An awesome react hooks library.',
})
const [cloned, setCloned, syncSource] = useClonedState(form)
// 直接修改克隆状态,即“修改表单”,数据与源状态隔离
const handleModify = (e: React.ChangeEvent<HTMLInputElement>) => {
setCloned((cloned) => ({
...cloned,
[e.target.name]: e.target.value,
}))
}
// 使用克隆状态的当前状态作为新的表单状态,即“保存修改”
const handleSave = () => setForm(cloned)
// 同步(重置)源状态与克隆状态,即“撤销修改”
const handleReset = () => syncSource()