This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Riot.js Cheatsheet

A quick reference guide for Riot.js: lightweight component library, custom tags, expressions, lifecycle events, and state updates.

· Published

Riot.js is a lightweight, elegant component-based UI library providing virtual DOM rendering with near-zero boilerplate.

Component Definition (my-component.riot)

<user-card>
  <div class="card">
    <h3>{ state.name }</h3>
    <p>Role: { props.role }</p>
    <button onclick="{ handleIncrement }">Likes: { state.likes }</button>
  </div>

  <script>
    export default {
      onBeforeMount(props, state) {
        this.state = {
          name: props.initialName || 'Anonymous',
          likes: 0
        };
      },
      handleIncrement() {
        this.update({
          likes: this.state.likes + 1
        });
      }
    }
  </script>
</user-card>

Lifecycle Events

Method Trigger Stage
onBeforeMount(props, state) Before component is mounted to the DOM
onMounted(props, state) After component is attached to the DOM
onBeforeUpdate(props, state) Before component state update re-renders
onUpdated(props, state) After component DOM updates
onBeforeUnmount(props, state) Right before component is destroyed
onUnmounted(props, state) After component has been removed from DOM

Loops & Conditionals

<!-- Conditionals -->
<p if="{ state.isLoggedIn }">Welcome back!</p>
<button if="{ !state.isLoggedIn }">Log In</button>

<!-- Loops -->
<ul>
  <li each="{ item in state.items }">
    { item.title }
  </li>
</ul>

References

Related topics