개체를 사용하여 데이터를 저장하는 저장소를 만들고 있습니다.
스프레드 연산자를 사용하여 상점을 업데이트 할 수 있지만 스프레드 연산자 없이도 업데이트 할 수 있습니다.
Svelte는 React와 같이 객체의 상태를 업데이트하기 전에 스프레드 연산자를 사용하여 새 객체를 만들어야하므로 원래 객체를 변경하지 않습니까?
withSpreadOperator()
또는 withoutSpreadOperator()
... 그게 질문입니다.
//stores.js
import { writable } from "svelte/store";
export const counts = writable({ n: 0 });
//App.js
<script>
import { count } from "./stores.js";
function withSpreadOperator() {
count.update(o => {
let x = { ...o };
x.n++;
return x;
});
}
function withoutSpreadOperator() {
count.update(o => {
o.n++;
return o;
});
}
</script>
<h1>The count is {$count.n}</h1>
<button on:click="{withSpreadOperator}">+</button>
<button on:click="{withoutSpreadOperator}">+</button>
둘 중 하나를 할 수 있습니다. Svelte는 신경 쓰지 않습니다. 당신이 선호하는 스타일에 달려 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다