mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Try improve crash course
This commit is contained in:
parent
c288cb92c4
commit
338c66ed57
11 changed files with 223 additions and 102 deletions
|
|
@ -29,13 +29,10 @@ local text = function()
|
|||
return "count: " .. tostring(count())
|
||||
end
|
||||
|
||||
effect(function()
|
||||
text() -- prints "ran"
|
||||
end)
|
||||
effect(function() text() end)
|
||||
effect(function() text() end)
|
||||
|
||||
effect(function()
|
||||
text() -- prints "ran" again
|
||||
end)
|
||||
source(1) -- prints "ran" x2
|
||||
```
|
||||
|
||||
To avoid this, you can use `derive()` to derive a new source instead. This will
|
||||
|
|
@ -55,16 +52,13 @@ local text = derive(function()
|
|||
return "count: " .. tostring(count())
|
||||
end)
|
||||
|
||||
effect(function()
|
||||
text() -- prints "ran"
|
||||
end)
|
||||
effect(function() text() end)
|
||||
effect(function() text() end)
|
||||
|
||||
effect(function()
|
||||
text() -- does not print, returns cached value
|
||||
end)
|
||||
source(1) -- prints "ran" x1
|
||||
```
|
||||
|
||||
`derive()` must also be used within a root reactive scope, just like `effect()`.
|
||||
`derive()` must also be called within a reactive scope, just like `effect()`.
|
||||
|
||||
If the recalculated value is the same as the old value, the derived source will
|
||||
not rerun the effects using it.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue