question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling hooks after inserting into the dom?

See original GitHub issue

At the moment, the behaviour of virtual-dom is to call hook on a hook once the dom element for the respective vnode has been created. However, if the dom element is newly created it has not yet been inserted into the dom, so has not been laid out and doesn’t have width or height properties. In order to make a basic hook that reads the width of a vnode once rendered you are forced to use a timeout like so:

hook: (element) ->
  # defer reading the width until the element has been inserted into the dom
  setTimeout ->
    width = $(element).outerWidth()
    # do something with width
  , 0

And deal with a whole load of dodgy asynchronicity (which makes testing hooks especially annoying). Is there a particular reason for this behaviour, or could we change it so that hook is called after insertion into the dom, avoiding the need to use timeouts in cases like the above?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
wclrcommented, Jan 16, 2016

+1 In cyclejs you can use CycleDOM’s observable API to know that element added to the dom. But think this is needed really for v-dom hooks/widgets eigher.

0reactions
udpcommented, Aug 11, 2016

+1

Ran into this trying to use codemirror as a widget and had to use the setTimeout 0 hack.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rules of Hooks - React
Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any...
Read more >
React hook that runs AFTER the DOM updates - Stack Overflow
An easy solution to this, is to manually check for state update in a useEffect hook: const myComponent = () => { const...
Read more >
Using React Hooks to Update DOM - Medium
Introducing React Hooks to update DOM dynamically and asynchronously. If you are new to React and are using classes all this while in...
Read more >
The Complete Guide to useRef() and Refs in React
How to use React.useRef() hook to create persisted mutable values (also known as references or refs), as well access DOM elements.
Read more >
How To Call Web APIs with the useEffect Hook in React
In this step, you'll create a local REST API using JSON server, which you will use as a test data source. Later, you'll...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found