Svelte rune store with map is not reactive #11881
Answered
by
brunnerh
baseplate-admin
asked this question in
Q&A
-
Hi, So i have this store // modals.svelte.ts
export function createModalStore() {
const modals = ["search"] as const;
type IModals = (typeof modals)[number];
let state = $state(new Map<IModals, boolean>(modals.map((item) => [item, false])));
return {
get state() {
return state;
},
get_modal_state(name: IModals) {
return state.get(name);
},
open_modal(name: IModals) {
state.set(name, true);
}
};
} I have a component <!-- $modals/index.svelte -->
<script lang="ts">
import { createModalStore } from "$stores/modals.svelte";
const modal_store = createModalStore();
$inspect(modal_store.state);
</script> and a <!-- +layout.svelte -->
<script lang='ts'>
import { createModalStore } from "$stores/modals.svelte";
import Modals from "$modals/index.svelte";
const modal_store = createModalStore();
</script>
<Modals />
<button
onclick={() => {
modal_store.open_modal("search");
}}
>
</button>
The underlying |
Beta Was this translation helpful? Give feedback.
Answered by
brunnerh
Jun 3, 2024
Replies: 1 comment 1 reply
-
import { Map } from 'svelte/reactivity';
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
baseplate-admin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$state
does not affect classes, only objects and arrays.If you need a reactive
Map
, there is an implementation that can be imported: