Skip to content

Event once rendering is complete #309

Answered by Tao-VanJS
ericraider33 asked this question in Q&A
Discussion options

You must be logged in to vote

A quick and dirty solution is to use setTimeout with a small delay. You can see this technique in a few places of the official codebase. For example: 1, 2. #123 is the thread that discusses the use of setTimeout.

If you want to get rid of setTimeout (thus the small delay introduced by it). You can program your app in this way (this solution requires VanJS 1.5.0 or later):

const {button, div} = van.tags

const Label = ({text, onRender}) => {
  if (onRender) {
    const trigger = van.state(false)
    van.derive(() => trigger.val && onRender())
    trigger.val = true
  }
  return div({class: "label"}, text)
}

const counter = van.state(0)

van.add(document.body,
  div(button({onclick: () => ++

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@ericraider33
Comment options

Answer selected by ericraider33
Comment options

You must be logged in to vote
2 replies
@ericraider33
Comment options

@Tao-VanJS
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants