Calling hooks after inserting into the dom?
See original GitHub issueAt 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:
- Created 8 years ago
- Comments:8
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

+1 In cyclejs you can use CycleDOM’s
observableAPI to know that element added to the dom. But think this is needed really for v-dom hooks/widgets eigher.+1
Ran into this trying to use codemirror as a widget and had to use the setTimeout 0 hack.