You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md
+41-41Lines changed: 41 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,7 @@ class Todo {
66
66
67
67
## `$state.frozen`
68
68
69
-
State declared with `$state.frozen`cannot be mutated; it can only be _reassigned_. In other words, rather than assigning to a property of an object, or using an array method like `push`, replace the object or array altogether if you'd like to update it:
69
+
`$state.frozen`で宣言された state は変更することができません、できるのは再代入だけです。言い換えると、もし更新したいのであれば、オブジェクトのプロパティに代入したり `push` のような配列のメソッドを使用するのではなく、オブジェクトや配列を完全に置き換える必要がある、ということです:
70
70
71
71
```diff
72
72
<script>
@@ -89,28 +89,28 @@ State declared with `$state.frozen` cannot be mutated; it can only be _reassigne
89
89
</p>
90
90
```
91
91
92
-
This can improve performance with large arrays and objects that you weren't planning to mutate anyway, since it avoids the cost of making them reactive. Note that frozen state can _contain_ reactive state (for example, a frozen array of reactive objects).
92
+
これによって、対象ををリアクティブにするコストを避けることができるため、変更するつもりのない大きな配列やオブジェクトを扱うときにパフォーマンスを改善することができます。凍結された(frozen) state にはリアテクティブな state を含めることができます(例えば、リアクティブなオブジェクトの凍結された配列、のように)。
93
93
94
-
> Objects and arrays passed to `$state.frozen`will be shallowly frozen using `Object.freeze()`. If you don't want this, pass in a clone of the object or array instead.
@@ -155,7 +155,7 @@ This is handy when you want to pass some state to an external library or API tha
155
155
156
156
## `$derived.by`
157
157
158
-
Sometimes you need to create complex derivations that don't fit inside a short expression. In these cases, you can use `$derived.by`which accepts a function as its argument.
@@ -197,18 +197,18 @@ In essence, `$derived(expression)` is equivalent to `$derived.by(() => expressio
197
197
<p>{count} doubled is {doubled}</p>
198
198
```
199
199
200
-
`$effect`will automatically subscribe to any `$state`or`$derived`values it reads _synchronously_ and reruns whenever their values change — that means, values after an `await`or inside a `setTimeout`will _not_ be tracked. `$effect`will run after the DOM has been updated.
200
+
`$effect`は 同期的に読み取った `$state`や`$derived`の値を自動的にサブスクライブし、値が変わるたびに再実行されます。なお、その値が `await`の後や `setTimeout`の中にある場合はその値は追跡されません。`$effect`は DOM が更新されたあとに実行されます。
201
201
202
202
```svelte
203
203
<script>
204
204
let count = $state(0);
205
205
let doubled = $derived(count * 2);
206
206
207
207
$effect(() => {
208
-
// runs after the DOM has been updated
209
-
// when the component is mounted
210
-
// and whenever `count` changes,
211
-
// but not when `doubled` changes,
208
+
// DOM が更新されたあとに実行されます。
209
+
// コンポーネントがマウントされるときや、
210
+
// `count` が変更されるたびに実行されますが、
211
+
// `doubled` が変更されたときには実行されません。
212
212
console.log(count);
213
213
214
214
setTimeout(() => console.log(doubled));
@@ -222,7 +222,7 @@ In essence, `$derived(expression)` is equivalent to `$derived.by(() => expressio
222
222
<p>{count} doubled is {doubled}</p>
223
223
```
224
224
225
-
An effect only reruns when the object it reads changes, not when a property inside it changes. If you want to react to _any_ change inside an object for inspection purposes at dev time, you may want to use [`inspect`](#$inspect).
@@ -258,23 +258,23 @@ An effect only reruns when the object it reads changes, not when a property insi
258
258
<p>{object.count} doubled is {derived_object.doubled}</p>
259
259
```
260
260
261
-
You can return a function from `$effect`, which will run immediately before the effect re-runs, and before it is destroyed ([demo](/#H4sIAAAAAAAAE42SzW6DMBCEX2Vl5RDaVCQ9JoDUY--9lUox9lKsGBvZC1GEePcaKPnpqSe86_m0M2t6ViqNnu0_e2Z4jWzP3pqGbRhdmrHwHWrCUHvbOjF2Ei-caijLTU4aCYRtDUEKK0-ccL2NDstNrbRWHoU10t8Eu-121gTVCssSBa3XEaQZ9GMrpziGj0p5OAccCgSHwmEgJZwrNNihg6MyhK7j-gii4uYb_YyGUZ5guQwzPdL7b_U4ZNSOvp9T2B3m1rB5cLx4zMkhtc7AHz7YVCVwEFzrgosTBMuNs52SKDegaPbvWnMH8AhUXaNUIY6-hHCldQhUIcyLCFlfAuHvkCKaYk8iYevGGgy2wyyJnpy9oLwG0sjdNe2yhGhJN32HsUzi2xOapNpl_bSLIYnDeeoVLZE1YI3QSpzSfo7-8J5PKbwOmdf2jC6JZyD7HxpPaMk93aHhF6utVKVCyfbkWhy-hh9Z3o_2nQIAAA==)).
// This will be recreated whenever `milliseconds` changes
269
+
// この interval は `milliseconds`が変更されるたびに再作成されます
270
270
const interval = setInterval(() => {
271
271
count += 1;
272
272
}, milliseconds);
273
273
274
274
return () => {
275
-
// if a callback is provided, it will run
276
-
// a) immediately before the effect re-runs
277
-
// b) when the component is destroyed
275
+
// コールバックが提供される場合、以下のときに実行されます
276
+
// a) effect が再実行される直前
277
+
// b) コンポーネントが破棄されるとき
278
278
clearInterval(interval);
279
279
};
280
280
});
@@ -288,21 +288,21 @@ You can return a function from `$effect`, which will run immediately before the
288
288
289
289
### When not to use `$effect`
290
290
291
-
In general, `$effect`is best considered something of an escape hatch — useful for things like analytics and direct DOM manipulation — rather than a tool you should use frequently. In particular, avoid using it to synchronise state. Instead of this...
291
+
一般的には、`$effect`は、頻繁に使用すべきツールというよりは、なんらかの (例えばアナリティクスや直接的な DOM 操作を行うのに便利な) エスケープハッチであると考えたほうがよいでしょう。特に、state の同期に使用するのは避けましょう。こうする代わりに…
292
292
293
293
```svelte
294
294
<script>
295
295
let count = $state(0);
296
296
let doubled = $state();
297
297
298
-
// don't do this!
298
+
// これをやってはいけません!
299
299
$effect(() => {
300
300
doubled = count * 2;
301
301
});
302
302
</script>
303
303
```
304
304
305
-
...do this:
305
+
…こうしましょう:
306
306
307
307
```svelte
308
308
<script>
@@ -311,12 +311,12 @@ In general, `$effect` is best considered something of an escape hatch — useful
311
311
</script>
312
312
```
313
313
314
-
> For things that are more complicated than a simple expression like `count * 2`, you can also use [`$derived.by`](#$derived-by).
When reacting to a state change and writing to a different state as a result, think about if it's possible to use callback props instead.
316
+
ある state の変更に対してリアクティブに反応し、その結果として別のステートに書き込む場合は、代わりにコールバック props が使用できないか検討しましょう。
317
317
318
318
```svelte
319
-
<!-- Don't do this -->
319
+
<!-- これをやってはいけません -->
320
320
<script>
321
321
let value = $state();
322
322
let value_uppercase = $state();
@@ -327,7 +327,7 @@ When reacting to a state change and writing to a different state as a result, th
327
327
328
328
<Text bind:value />
329
329
330
-
<!-- Do this instead: -->
330
+
<!-- 代わりにこうしましょう: -->
331
331
<script>
332
332
let value = $state();
333
333
let value_uppercase = $state();
@@ -340,7 +340,7 @@ When reacting to a state change and writing to a different state as a result, th
340
340
<Text {value} {onValueChange}>
341
341
```
342
342
343
-
If you want to have something update from above but also modify it from below (i.e. you want some kind of "writable `$derived`"), and events aren't an option, you can also use an object with getters and setters.
@@ -358,7 +358,7 @@ If you want to have something update from above but also modify it from below (i
358
358
<input bind:value={facade.value} />
359
359
```
360
360
361
-
If you absolutely have to update `$state`within an effect and run into an infinite loop because you read and write to the same `$state`, use [untrack](functions#untrack).
0 commit comments