DEV Community

Lam
Lam

Posted on

3

Riot.Js Cheat Sheet

Lifecycle

this.on('before-mount', function() {
// before the tag is mounted
})

this.on('mount', function() {
// right after the tag is mounted on the page
})

this.on('update', function() {
// allows recalculation of context data before the update
})

this.on('updated', function() {
// right after the tag template is updated
})

this.on('before-unmount', function() {
// before the tag is removed
})

this.on('unmount', function() {
// when the tag is removed from the page
})

// curious about all events ?
this.on('all', function(eventName) {
console.info(eventName)
})
Enter fullscreen mode Exit fullscreen mode

Router

riot.route('customers/*/edit', (id) => {
})
riot.route('customers/234/edit')
riot.route.start()
riot.route.start(true) // exec the current url
Enter fullscreen mode Exit fullscreen mode

[Nested HTML] Yield to/from

<post>
  <yield to='title'>Hello</yield>
  <yield to='body'>Hey there world</yield>
</post>
Enter fullscreen mode Exit fullscreen mode
<post>
  <yield from='title'/>
  <yield from='body'/>
</post>
Enter fullscreen mode Exit fullscreen mode

Nested HTML

<yield/>
Enter fullscreen mode Exit fullscreen mode

[Nesting] Names

<my-tag>
  <child name='xyz'></child>
  var child = this.tags.xyz
</my-tag>
Enter fullscreen mode Exit fullscreen mode

Nesting

<my-tag>
  <child></child>
  var child = this.tags.child
</my-tag>
Enter fullscreen mode Exit fullscreen mode

API

this.update()
this.update({ data: 'hi' }

this.unmount()
this.unmount(true) // keep parent tag

riot.update() // update all
Enter fullscreen mode Exit fullscreen mode

[Expressions] Events

<button onclick={go}>

this.go = function (e) { ... }
Enter fullscreen mode Exit fullscreen mode

[Expressions] Conditional

<div if={error}>
<div show={error}> /* show using display: '' */
<div hide={error}> /* hide using display: none */
Enter fullscreen mode Exit fullscreen mode

[Expressions] Loops

<li each={movies}>{title}</li>
Enter fullscreen mode Exit fullscreen mode

Reference

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay