In Svelte, is #key the proper way to make a block reactive if it makes no local reference to variables?

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

In Svelte, is #key the proper way to make a block reactive if it makes no local reference to variables?

问题

在下面的代码中,我调用一个函数(getFormattedValue)来获取要显示的值。这个函数是对另一个函数(node.getFormattedAttrValue)的包装,它提供了一个共同的参数 data,这样我就不必为每个要显示的属性重复使用数据参数了。每个属性只需提供其名称。但在这样做之后,组件的主体不再响应(例如,在数据变化时)。显然,在{}标记中没有data使它们不具有响应性。但我不想一直重复相同的参数。那么该怎么办?

<script>
  export let data, node
  const getFormattedValue = (synonym) => node.getFormattedAttrValue(data, synonym)
</script>

<Card>
  <span slot="Body">
    <li>{getFormattedValue("CURRENT_VALUE")}</li>
    <li>{getFormattedValue("MIN_VALUE")}</li>
    <li>{getFormattedValue("MAX_VALUE")}</li>
    <li>{getFormattedValue("PCT_CONSUMED")}%</li>
  </span>
</Card>

我唯一能够让它重新响应而不将data重新放入参数列表的方法是添加一个key块:

{#key data}
  <li>{getFormattedValue("CURRENT_VALUE")}</li>
  <li>{getFormattedValue("MIN_VALUE")}</li>
  <li>{getFormattedValue("MAX_VALUE")}</li>
  <li>{getFormattedValue("PCT_CONSUMED")}%</li>
{/key}

但我了解到key会完全销毁一切并重新构建,因此它似乎过于重-handed。有没有更好的方法来做到这一点?

英文:

In the code below, I am calling a function (getFormattedValue) to obtain the value to display. That is a wrapper around another function (node.getFormattedAttrValue) that supplies a common parameter, data so I don't have to keep repeating the data parameter for each attribute I'm displaying... the only thing each must provide is its name. But after doing this, the component body stopped reacting (e.g. on change to data). Apparently not having data in the { } tags made them non-reactive. But I don't want to keep repeating that same parameter. So what to do?

&lt;script&gt;
  export let data, node
  const getFormattedValue = (synonym) =&gt; node.getFormattedAttrValue(data,synonym)

&lt;/script&gt;

&lt;Card&gt;
  &lt;span slot=&quot;Body&quot;&gt;
    &lt;li&gt;{getFormattedValue(&quot;CURRENT_VALUE&quot;)}&lt;/li&gt;
    &lt;li&gt;{getFormattedValue(&quot;MIN_VALUE&quot;)}&lt;/li&gt;
    &lt;li&gt;{getFormattedValue(&quot;MAX_VALUE&quot;)}&lt;/li&gt;
    &lt;li&gt;{getFormattedValue(&quot;PCT_CONSUMED&quot;)}%&lt;/li&gt;
  &lt;/span&gt;
&lt;/Card&gt;

The only way I could get it to react again without putting data back into the parameter list is to add a key block on it:

  {#key data}
    &lt;li&gt;{getFormattedValue(&quot;CURRENT_VALUE&quot;)}&lt;/li&gt;
    &lt;li&gt;{getFormattedValue(&quot;MIN_VALUE&quot;)}&lt;/li&gt;
    &lt;li&gt;{getFormattedValue(&quot;MAX_VALUE&quot;)}&lt;/li&gt;
    &lt;li&gt;{getFormattedValue(&quot;PCT_CONSUMED&quot;)}%&lt;/li&gt;
   {/key}

But I read that key completely destroys everything and rebuilds it, so it sounds rather heavy-handed. Is there a better way to do this?

答案1

得分: 3

你可以以响应式的方式定义函数:

$: getFormattedValue = (synonym) => node.getFormattedAttrValue(data, synonym)

REPL 示例

英文:

You can define functions reactively as well:

$: getFormattedValue = (synonym) =&gt; node.getFormattedAttrValue(data,synonym)

REPL example

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

发表评论

匿名网友

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

确定