Skip to content
Mithril.js 2
Main Navigation GuideAPI

English

简体中文
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

English

简体中文
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

Appearance

Sidebar Navigation

Getting Started

Installation

Tutorial

Resources

JSX

ES6+ on legacy browsers

Animation

Testing

Examples

3rd Party Integration

Path Handling

Key concepts

Vnodes

Components

Lifecycle methods

Keys

Autoredraw system

Misc

Framework comparison

Migrating from v1.x

Migrating from v0.2.x

API

On this page

How to read signatures ​

Signature sections typically look like this:

vnode = m(selector, attributes, children)

ArgumentTypeRequiredDescription
selectorString|ObjectYesA CSS selector or a component
attributesObjectNoHTML attributes or element properties
childrenArray<Vnode>|String|Number|BooleanNoChild vnodes. Can be written as splat arguments
returnsVnodeA vnode

The signature line above the table indicates the general syntax of the method, showing the name of the method, the order of its arguments and a suggested variable name for its return value.

The Argument column in the table indicates which part of the signature is explained by the respective table row. The returns row displays information about the return value of the method.

The Type column indicates the expected type for the argument.

A pipe (|) indicates that an argument is valid if it has any of the listed types. For example, String|Object indicates that selector can be a string OR an object.

Angled brackets (< >) after an Array indicate the expected type for array items. For exampe, Array<String> indicates that the argument must be an array and that all items in that array must be strings. Angled brackets after an Object indicate a map. For example, Object<String,Component> indicates that the argument must be an object, whose keys are strings and values are components

Sometimes non-native types may appear to indicate that a specific object signature is required. For example, Vnode is an object that has a virtual DOM node structure.

The Required column indicates whether an argument is required or optional. If an argument is optional, you may set it to null or undefined, or omit it altogether, such that the next argument appears in its place.

Optional arguments ​

Function arguments surrounded by square brackets [ ] are optional. In the example below, url is an optional argument:

m.request([url,] options)

Splats ​

A splat argument means that if the argument is an array, you can omit the square brackets and have a variable number of arguments in the method instead.

In the example at the top, this means that m("div", {id: "foo"}, ["a", "b", "c"]) can also be written as m("div", {id: "foo"}, "a", "b", "c").

Splats are useful in some compile-to-JS languages such as CoffeeScript, and also allow helpful shorthands for some common use cases.

Function signatures ​

Functions are denoted with an arrow (->). The left side of the arrow indicates the types of the input arguments and the right side indicates the type for the return value.

For example, parseFloat has the signature String -> Number, i.e. it takes a string as input and returns a number as output.

Functions with multiple arguments are denoted with parenthesis: (String, Array) -> Number

Component signatures ​

Components are denoted via calls to m, but with the initial selector argument set to a constant named in the relevant prose:

vnode = m(m.route.Link, attributes, children)

ArgumentTypeRequiredDescription
attributes.hrefObjectYesThe target route to navigate to.
attributes.selectorString|Object|FunctionNoThis sets the tag name to use. Must be a valid selector for m if given, defaults to "a".
attributes.optionsObjectNoThis sets the options passed to m.route.set.
attributesObjectNoOther attributes to apply to the returned vnode may be passed.
childrenArray<Vnode>|String|Number|BooleanNoChild vnodes for this link.
returnsVnodeA vnode.

Children here, if specified, are assumed to be able to be written as splat arguments, unless otherwise specified in prose.

An element with no sensible children and/or attributes may choose to elide the relevant parameter entirely:

vnode = m(Component, attributes)

ArgumentTypeRequiredDescription
attributes.hrefObjectYesThe
attributesObjectNoOther attributes to apply to the returned vnode
returnsVnodeA vnode
Pager
Next pageInstallation

Released under the MIT License.

Copyright (c) 2024 Mithril Contributors

https://mithril.js.org/signatures.html

Released under the MIT License.

Copyright (c) 2024 Mithril Contributors