Skip to content
0:00 / 0:00

#svelte#tick

Svelte Tutorial for Beginners - tick()

lihautan

9.7K subscribers • 1 video on Hallway

YouTube
  • Recommended by 1 person: 1 on Stack Overflow and its sister sites.
  • Presented by Li Hau Tan

Beginner. No background in the topic needed.

4.6K views5 years ago1 recommendation

Web developmentVue, Svelte, Angular and othersWeb performance

Li Hau Tan explains how to use the Svelte tick function to wait for pending state updates to be applied to the DOM.

We've learned about reactivity and reactive declarations and statements, it's time to know about the tick() function tick() allows you to wait for all pending updates to be applied to the DOM. In this video, I am showing 2 examples of how you could use tick() in your Svelte component. Introduction First example - wait for reactive declared variables to be updated Second example - wait for updates to restore element state --- 💻 REPL svelte.dev/repl/a0e8adb56903414283e57d2328c3745b?version=3.3… 🎧 Svelte Tutorial for Beginners youtube.com/playlist?list=PLoKaNN3BjQX3mxDEVG3oGJx2ByXnue_gR 📚 Related Resources Svelte svelte.dev Svelte REPL svelte.dev/repl Svelte Tutorial: tick() svelte.dev/tutorial/tick 🥰 Support me buymeacoffee.com/lihautan 🔗 Want to learn more from me? Check out these links: Twitter: twitter.com/lihautan Blog: lihautan.com/ Newsletter: lihautan.com/newsletter #svelte #sveltetutorial #sveltebeginner Thanks for watching 🥰 -- with love, Li Hau

Why it's here

1 person recommends this

What people say. Recommended as a clear explanation of how Svelte batches DOM updates and how to use tick to wait for them.

Tan Li Hauon Stack Overflow • Recommends

On “Understanding Svelte tick() lifecycle”

When you update your (reactive) variables, it does not immediately reflect onto the DOM, the updates are batched together with other changes and updates in the next update cycle. Much like react, if you do this.setState({ foo: 123 }); this.state.foo // you don't expect to read 123 // because this.setState is asynchronous! Let's take a look at 1 example, and see how we do it in react vs svelte Say you have a growing text input, which as you type, should grow in height, to accommodate more text. Here's how you'd probably do it in React: function GrowingTextInput() { const ref = useRef(); const value, setValue = useState(''); const updateText = (newValue) => { setValue(newVa…