-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
view doesn't update when changing state in setInterval #243
Comments
This is expected. Karax's reactivity model is different to mainstream frameworks (like React and Vue). They implement it by creating reactive state with useState/setState etc. Karax instead reacts to events, similar to things like Elm. This approach is perhaps simpler and easier to reason about, with the tradeoff being that events need to be wrapped to trigger a redraw. Karax does this for you with dom event handlers, ajax network calls, etc, but you will need to add it for things outside of that (websocket messages, document timing functions). So the recommended solution would be something like: include karax/prelude
import karax/kdom except setInterval
proc setInterval(cb: proc(), interval: int): Interval =
kdom.setInterval(proc =
cb()
if not kxi.surpressRedraws: redraw(kxi)
, interval)
var v = 10
proc update() =
v += 10
discard setInterval(update, 200)
proc main(): VNode =
result = buildHtml(tdiv):
text $v
setRenderer main Does that all make sense? |
I got it. Many thanks, @geotre, your explanation is crystal clear. |
Thanks for the suggestion @Athlon64, I have added it! |
Minimal code as follows:
The view won't get update unless calling redraw() explicitly.
The text was updated successfully, but these errors were encountered: