[英]How can I get rid of Reference error using music-metadata in electron app
[英]How can I get rid of the reference of the object?
我使用 React 鈎子管理存儲我的狀態。 當我 CRUD 操作是這樣的
const {state} = useContext(myContext);
const CRUD = async () => {
state.entity.id = generateId();
state.entity.name = "bla bla";
state.entity.status = true;
.... POSTING PROCCESS
}
之后的操作,它改變了沒有減速器的狀態。 我不明白,我不想改變。
我嘗試了一些示例來解決此問題,但沒有奏效。
let clone = {...state.entity}; // not work
var copy = Object.assign({}, state.entity); // not work
const other = {...state.entity}; // not work
//.....or other same thing not work. (const, var, let combination)
展開運算符僅創建對象的淺層克隆。
你可以這樣做:
const clone = JSON.parse(JSON.stringify(state.entity))
這將復制您的狀態,但不會復制其中的任何對象方法或原型。
所以,在改變狀態時你應該總是使用setState()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.