I'm wondering about a component and how its local variables that capture a computed result from a store, and how reactivity comes in.
If I have a store defined like so:
export const getInitialState = (): RoundState => ({
playerNames: [],
locked: false,
finished: false,
questions: [],
picksChosen: false,
roundPicks: [],
})
export const round = writable<RoundState>(getInitialState())
export const questions = derived(round, (round) => round.questions)
And then inside a component i want to use the questions array as a starting point, using it to create local form data:
<script lang="ts">
import { onMount } from 'svelte'
import {
questions
} from '../../lib/stores/round'
function loadData() {
// fetches data from an api that update's round store
getRoundStatus()
}
let answers: Answers
answers = $questions.map((q) => ({
id: q.id,
label: q.body,
value: '',
}))
</script>
{#each answers as answer}
<select />
{/each}
My thinking is that because i'm using a local variable that assigns to the auto subscription, it should work: https://svelte.dev/docs#component-format-script-2-assignments-are-reactive. However in my test suite i'm finding this is not the case.
If i update it to be a reactive statement, it's fine.
let answers: Answers = $questions.map((q) => ({
id: q.id,
label: q.body,
value: '',
}))
However given the docs example i posted, I feel like this shouldn't be needed. So my question here is if i'm doing something incorrect.