Components let you bundle up javascript functions and use them across your <ui-state> elements. Perfect for reusable widgets and complex interactions.
Register your component
MiniJS.register('counter', {
increment() {
this.state.count++
}
decrement() {
this.state.count--
}
})
Use it in your HTML
<ui-state component="counter" count="25">
<div ui-text="count"></div>
<button ui-click="increment">+</button>
<button ui-click="decrement">-</button>
</ui-state>
Inside component methods, you can access:
this - refers to the component instance.this.state - the component's state object.this.refs - references to DOM elements with the ref attribute.this.customMethod() - call other methods defined in the component.init() to set up your componentThe init() method runs automatically when your component loads:
MiniJS.register('todo-app', {
init() {
this.state.todos = []
this.state.newTodo = ''
}
})
Define methods in your component and call them with ui-click, ui-change, etc:
MiniJS.register('todo-app', {
init() {
this.state.todos = [
{ text: 'Learn Mini.js' },
{ text: 'Build a todo app' },
]
this.state.newTodo = ''
},
addTodo() {
const newTodo = this.state.newTodo.trim();
if (!newTodo) return;
this.state.todos.push(newTodo)
this.state.newTodo = ''
}
})
<ui-state component="todo-app" todos new-todo>
<input ui-value="newTodo" ui-change="newTodo = this.value" />
<button ui-click="addTodo">Add</button>
</ui-state>
Any attribute on <ui-state> becomes state (except id, class, component, and style):
<ui-state component="user-form" first-name="John" last-name="Doe">
<input ui-value="firstName" />
<input ui-value="lastName" />
</ui-state>
Use the ref attribute to access DOM elements directly in your component methods:
<ui-state component="list-manager" items>
<div ref="output"></div>
<button ui-click="addItem">Add</button>
</ui-state>
MiniJS.register('list-manager', {
init() {
this.state.items = ['Item 1', 'Item 2']
this.render()
},
addItem() {
this.state.items.push(`Item ${this.state.items.length + 1}`)
this.render()
},
render() {
const output = this.refs.output
output.innerHTML = this.state.items
.map(item => `<li>${item}</li>`)
.join('')
}
})
Use components when:
Stick with regular <ui-state> when:
Components are just a way to package up javascript functions and reuse them across your <ui-state> elements. Start simple and add complexity only when you need it.