首页 > 解决方案 > 如何更新这个 Svelte 商店而不每次都重新创建它?

问题描述

这里是 REPL:https ://svelte.dev/repl/56770fec88af4b76bdc8ea962178854e?version=3.42.1

这里的代码:

App.svelte:

<script>
    import {editableStore} from "./store";
    
    let name = "John"

    $: player = editableStore(name);
</script>

<h1>Hello {$player.name}!</h1>

<button on:click={() => name = (name === "Bob" ? "Jerry" : "Bob")}>
    Change name
</button>

<h2>Log:</h2>

{#each $player.log as log}
    <li>{log}</li>
{/each}

商店.js:

import {writable} from "svelte/store";

const defaultStore = {
    name: "Bob",
    age: 18,
    log: []
};

export const editableStore = (name) => {
    console.log("Recreated with name:", name);

    const {subscribe, update} = writable({...defaultStore}, () => () => clearInterval);

    if (name) {
        update(s => ({...s, name}));
    }

    const clearInterval = setInterval(() => {
        update(s => ({...s, log: [...s.log, new Date()]}))
    }, 1000)

    return { subscribe };
};

如您所见,如果您单击“更改名称”,商店将重新创建。

这是我需要避免的。

但是怎么做?

标签: javascriptsveltesvelte-3svelte-componentsvelte-store

解决方案


与其每次name更改都重新创建商店,不如只创建一次并$player.namename更改时设置。

<script>
    import {editableStore} from "./store";
    
    let name = "John";

    let player = editableStore(name);
    $: $player.name = name;
</script>

这将要求您更新您的 store 方法以返回该set函数。

export const editableStore = (name) => {
    console.log("Recreated with name:", name);

    // also destructure set here
    const {subscribe, update, set} = writable({...defaultStore}, () => () => clearInterval);

    if (name) {
        update(s => ({...s, name}));
    }

    const clearInterval = setInterval(() => {
        update(s => ({...s, log: [...s.log, new Date()]}))
    }, 1000)

    // also return set here
    return { subscribe, set };
};

推荐阅读