在Svelte中,当一个变量发生改变时,是否可以重新渲染/更新一个each块?

huangapple go评论63阅读模式
英文:

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}

&lt;button class={getGuessInfo(i).state}
        on:click={() =&gt; changeColor(index)}&gt;{getGuessInfo(i).letter}
&lt;/button&gt;

{/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 &gt; $Guess.length - 1) return {letter: &quot;&quot;, 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 &amp;&amp; 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 =&gt;
    index &gt; $Guess.length - 1 ?
      { letter: &quot;&quot;, 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)

huangapple
  • 本文由 发表于 2023年2月9日 00:40:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定