英文:
Is it possible to rerender/update an each block in Svelte when a variable changes?
问题
So, I'm pretty new to svelte and very new to sveltekit, but ever since I started learning svelte I've always seemed to run into this problem one way or another.
Here, I have an each block:
{#each {length: wordLength} as _, i}
<button class={getGuessInfo(i).state}
on:click={() => changeColor(index)}>{getGuessInfo(i).letter}
</button>
{/each}
And I want to basically have this be reactive to my getGuessInfo()'s return value. I get you can make a function reactive if it doesn't have any parameters, but I'm not sure how to / if it's even possible to if the function relies on an index value.
Here is the getGuessInfo()
definition:
function getGuessInfo(index) {
if (index > $Guess.length - 1) return {letter: "", state: LetterState.MISS}
return $Guess[index]
}
This basically just returns a blank letter/state if the index is too high, and the letter/state of Guess at that position if not.
This works fine when I open the website, but changing the value of Guess yields no reactivity from the page.
Honestly, I'm so new to frameworks/web development in general that I'm not really sure what I want as a solution, but maybe something like just rerendering the component? Or just the each block? Not sure.
Thanks!
Well, I tried doing:
$: $Guess && getGuessInfo()
But that obviously didn't work
I previously just had an each block that was directly tied to Guess, as in:
{#each $Guess as btn, index}
And that worked fine
Thing is I want to constantly have 5 (or wordLength) buttons showing at all times, even if they're blank. That's why I tried using the function
英文:
So, I'm pretty new to svelte and very new to sveltekit, but ever since I started learning svelte I've always seemed to run into this problem one way or another.
Here, I have an each block:
{#each {length: wordLength} as _, i}
<button class={getGuessInfo(i).state}
on:click={() => changeColor(index)}>{getGuessInfo(i).letter}
</button>
{/each}
And I want to basically have this be reactive to my getGuessInfo()'s return value. I get you can make a function reactive if it doesn't have any parameters, but I'm not sure how to / if it's even possible to if the function relies on an index value.
Here is the getGuessInfo()
definition:
function getGuessInfo(index) {
if (index > $Guess.length - 1) return {letter: "", state: LetterState.MISS}
return $Guess[index]
}
This basically just returns a blank letter/state if the index is too high, and the letter/state of Guess at that position if not.
This works fine when I open the website, but changing the value of Guess yields no reactivity from the page.
Honestly, I'm so new to frameworks/web development in general that I'm not really sure what I want as a solution, but maybe something like just rerendering the component? Or just the each block? Not sure.
Thanks!
Well, I tried doing:
$: $Guess && getGuessInfo()
But that obviously didn't work
I previously just had an each block that was directly tied to Guess, as in:
{#each $Guess as btn, index}
And that worked fine
Thing is I want to constantly have 5 (or wordLength) buttons showing at all times, even if they're blank. That's why I tried using the function
答案1
得分: 2
The reactive attempt is not too far off, you probably could define the function reactively like this:
$: getGuessInfo = index =>
index > $Guess.length - 1 ?
{ letter: "", state: LetterState.MISS } :
$Guess[index];
This should cause it to be re-evaluated when $Guess
changes.
(Ternary operator just for brevity, you could use a block & return
statements.)
英文:
The reactive attempt is not too far off, you probably could define the function reactively like this:
$: getGuessInfo = index =>
index > $Guess.length - 1 ?
{ letter: "", state: LetterState.MISS } :
$Guess[index];
This should cause it to be re-evalutated when $Guess
changes.
(Ternary operator just for brevity, you could use a block & return
statements.)
答案2
得分: 1
为了使函数调用的结果具有响应性,需要将应该更新它的基于的变量作为参数添加 - 在您的情况下
getGuessInfo(i, $Guess)
(将其不仅添加到函数调用中,还添加到函数定义中会更清晰,但如果该变量在作用域内,这不是必需的)
英文:
To make the result of a function call reactive, add the variable based on which it should update as parameter - in your case
getGuessInfo(i, $Guess)
(Would be cleaner to not only add it in the function call, but also in the function definition. But if the variable is in scope, that's not essential)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论