useState 是用来保存自定义对象的吗?
Is useState meant to hold a custom object?
const [obj, set_obj] = useState(new CusObj())
持有原始对象和自定义对象的 useState 之间有什么区别吗?
来自docs
Unlike the setState method found in class components, useState does
not automatically merge update objects. You can replicate this
behavior by combining the function updater form with object spread
syntax:
setState(prevState => {
// Object.assign would also work
return {...prevState, ...updatedValues};
});
Another option is useReducer, which is more suited for managing state
objects that contain multiple sub-values.
对于复杂的状态值,我会改用 useReducer 挂钩,但简而言之 是的 你可以。
const [obj, set_obj] = useState(new CusObj())
持有原始对象和自定义对象的 useState 之间有什么区别吗?
来自docs
Unlike the setState method found in class components, useState does not automatically merge update objects. You can replicate this behavior by combining the function updater form with object spread syntax:
setState(prevState => { // Object.assign would also work return {...prevState, ...updatedValues}; });
Another option is useReducer, which is more suited for managing state objects that contain multiple sub-values.
对于复杂的状态值,我会改用 useReducer 挂钩,但简而言之 是的 你可以。