Core API

riot.mount

riot.mount(selector: string, props?: object, componentName?: string): [RiotComponent]

  • selector selects elements from the page and mounts them with a custom components. The selected elements’ name must match the custom tag name. DOM nodes having the is attribute can also be automounted

  • props optional object is passed for the component to consume. This can be anything, ranging from a simple object to a full application API. Or it can be a Flux- store. Really depends on how you want to structure your client-side applications. Also note that attributes you set on your tags will take precedence over ones specified with same names via props argument.

  • componentName optional component name in case the node to mount can’t be automounted by riot

@returns: an array of the mounted component objects

Examples:

  1. // selects and mounts all <pricing> tags on the page
  2. const components = riot.mount('pricing')
  3. // mount all tags with a class name .customer
  4. const components = riot.mount('.customer')
  5. // mount <account> tag and pass an API object as options
  6. const components = riot.mount('account', api)
  7. // mount <div id="root"> tag passing the API object using the previously registered `app` component
  8. const components = riot.mount('#root', api, 'app')

Note: users of In-browser compilation will need to wait the components compilation before calling the riot.mount method.

  1. (async function main() {
  2. await riot.compile()
  3. const components = riot.mount('user')
  4. }())

The props argument can be also a function in order to avoid sharing the same object across several tag instances riot/2613

  1. riot.mount('my-component', () => ({
  2. custom: 'option'
  3. }))

riot.unmount

riot.unmount(selector: string, keepRootElement?: boolean): [HTMLElement]

  • selector selects elements from the page and unmounts them if they were mounted before.
  • keepRootElement boolean optional parameter that can be used to avoid removing the root nodes from the DOM >=4.3.0
  1. // Select all the <user> tags and unmount them
  2. riot.unmount('user')

If the keepRootElement parameter will be true the root nodes will be left into the DOM

  1. // Select all the <user> tags, unmount them but leave their root nodes into the DOM
  2. riot.unmount('user', true)

@returns: an array of the mounted component objects

riot.component

riot.component(component: RiotComponentShell): function

The riot.component method can be used to create and mount component without registering them globally:

  1. import * as riot from 'riot'
  2. import App from './app.riot'
  3. const createApp = riot.component(App)
  4. const app = createApp(document.getElementById('root'), {
  5. name: 'This is a custom property'
  6. })

riot.install

riot.install(plugin: function): Set

  • plugin - function receiving a component objects for any component created@returns: a javascript Set containing all the plugin functions installed

Once installed a plugin function will be called for any Riot.js component created

  1. import { install } from 'riot'
  2. let id = 0
  3. // this plugin adds the uid attribute on any riot component created
  4. install(function(component) {
  5. component.uid = id++
  6. return component
  7. })

riot.uninstall

riot.uninstall(plugin: function): Set

  • plugin - function plugin already installed before@returns: a javascript Set containing all the plugin remaining functions installed

A plugin can be installe and uninstalled:

  1. import { install, uninstall } from 'riot'
  2. import uid from './riot-uid.js'
  3. install(uid)
  4. // uninstall the plugin if it's not needed anymore
  5. uninstall(uid)

riot.register

riot.register(name: string, component: RiotComponentShell): Map

  • name - the component name
  • component - a component shell object@returns: a javascript Map containing all registered components factory functions
  1. import { register, mount } from 'riot'
  2. import MyComponent from './my-component.riot'
  3. // register the my-component as global component
  4. register('my-component', MyComponent)
  5. // find all the DOM nodes called `<my-component>` and
  6. // mount them with the component previously registered
  7. mount('my-component')

riot.unregister

riot.unregister(name: string): Map

  • name - the component name@returns: a javascript Map containing the remaining registered components factory functions

Unregistering a tag previously created via compiler or via riot.register()This method could be handy in case you need to test your app and you want to create multiple tags using the same name for example

  1. import { register, unregister } from 'riot'
  2. import TestComponent from './test-component.riot'
  3. import TestComponent2 from './test-component2.riot'
  4. // create a test component
  5. register('test-component', TestComponent)
  6. // mount it
  7. const [component] = mount(document.createElement('div'), 'test-component')
  8. expect(component.root.querySelector('p')).to.be.ok
  9. // unregister the tag
  10. unregister('test-component')
  11. // recreate the same component using a different template
  12. register('test-component', TestComponent2)

riot.version

riot.version(): string

@returns: the current riot version in use as string