Custom components inside the of the page needs to be closed normally: <my-component></my-component>
and self-closing: <my-component/>
is not supported.
Some example uses of the mount method:
// mount an element with a specific id
riot.mount('#my-element')
// mount selected elements
riot.mount('todo, forum, comments')
A document can contain multiple instances of the same component.
Riot gives you access to your component DOM elements via this.$
and this.$$
helper methods.
<my-component>
<h1>My todo list</h1>
<ul>
<li>Learn Riot.js</li>
<li>Build something cool</li>
</ul>
<script>
export default {
onMounted() {
const title = this.$('h1') // single element
const items = this.$$('li') // multiple elements
}
}
</script>
</my-component>
How to use jQuery, Zepto, querySelector, etc.
Now that we know how to get DOM elements in the onUpdated
or onMounted
callbacks, we can make this useful by also adding a context to our element queries to the root element
(the riot tag we’re creating).
<my-component>
<p id="findMe">Do I even Exist?</p>
<p>Is this real life?</p>
<script>
export default {
onMounted() {
// Contexted jQuery
$('p', this.root) // similar to this.$
// Contexted Query Selector
this.root.querySelectorAll('p') // similar to this.$$
}
}
</script>
</my-component>
Properties
You can pass initial properties for components in the second argument
<script>
riot.mount('todo', { title: 'My TODO app', items: [ ... ] })
</script>
The passed data can be anything, ranging from a simple object to a full application API. Or it can be a Redux store. Depends on the designed architecture.
Inside the tag the properties can be referenced with the this.props
attribute as follows:
<my-component id="{ state.name }-{ state.surname }">
<p>{ state.name } - { state.surname }</p>
<script>
export default {
onMounted() {
// this is good but doesn't update the component DOM
this.state.name = 'Jack'
// this call updates the state and the component DOM as well
this.update({
surname: 'Black'
}
</script>
</my-component>
Riot component lifecycle
A component is created in following sequence:
- The component object is created
- The javascript logic is executed
- All HTML expressions are calculated
The component DOM is mounted on the page and “onMounted” callback is calledAfter the component is mounted the expressions are updated as follows:
When
this.update()
is called on the current component instance- When
this.update()
is called on a parent component, or any parent upwards. Updates flow uni-directionally from parent to child.The “onUpdated” callback is called every time component tag is updated.
Since the values are calculated before mounting there are no surprise issues such as failed <img src={ src }>
calls.
<my-component>
<script>
export default {
onBeforeMount(props, state) {
// before the component is mounted
},
onMounted(props, state) {
// right after the component is mounted on the page
},
onBeforeUpdate(props, state) {
// allows recalculation of context data before the update
},
onUpdated(props, state) {
// right after the component template is updated after an update call
},
onBeforeUnmount(props, state) {
// before the component is removed
},
onUnmounted(props, state) {
// when the component is removed from the page
}
}
</script>
Any callback receives always the current this.props
and this.state
as arguments.