snabbmitt
v0.0.16
Published
Stateful components in Snabbdom using Mitt
Downloads
16
Maintainers
Readme
snabbmitt
Stateful components in Snabbdom using Mitt and a subtree patching algorithm implementation
What is the idea?
As you may know, Snabbdom is a really nice virtual DOM library, indeed, is enough mature and extensible to create more complex structures like
Stateful Components
with a composable, clear and fractal approach.
On the other hand, a stateful component needs a way to manage their internal state and redraw. So, in that case, I think we can use simple event emitters and that's why Mitt is the best choice.
You can try the particles example
It's better to understand with an example. Let's go to create a clock app.
Creating the view for our app
const { snabbmitt } = require('snabbmitt');
const { run } = snabbmitt();
const h = require('snabbdom/h').default;
function App() {
function view() {
return h('h1', [
h('span.hours', '00'),
':',
h('span.minutes', '00'),
':',
h('span.seconds', '00')
]);
}
return view;
}
run(document.getElementById('app'), App);
DEMO
- We got the snabbmitt module
- Then we created an instance of the
snabbdom patch
that returns arun
function to mount our app in the DOM. - I always prefer hyperscript over other alternatives like JSX so in these examples, I'm going to use the
h
snabbdom module. - What about the application? Well, it's only a named function that acts as a
function factory
for ourcomponent
and returns aview
function. So, theApp
function is our first component.
Creating a state to keep tracking the hours, minutes and seconds
function App() {
function store() {
const state = {
hours: 0,
minutes: 0,
seconds: 0
};
return state;
}
function displayDigit(digit) {
if (digit < 10) {
return `0${digit}`;
}
return digit;
}
function view({ state }) {
return h('h1', [
h('span.hours', displayDigit(state.hours)),
':',
h('span.minutes', displayDigit(state.minutes)),
':',
h('span.seconds', displayDigit(state.seconds))
]);
}
return {
view,
store
};
}
DEMO
- We defined a new function inside our component called
store
. This function is in charge of creating the state and each behavior related with their mutable updates (like reducers in redux but different, here we are talking about mutable updates). - Now our view function can use the state as a destructuring object argument.
- Notice too that our App return an object
{ view, store }
instead of only the view function as before.
Creating the behaviors for our state and how to update it. We are going to use event emitters :)
function App({ emitter }) {
function store() {
const state = {
hours: 0,
minutes: 0,
seconds: 0
};
emitter.on('clock:update', () => {
state.seconds++;
if (state.seconds === 60) {
state.minutes++;
state.seconds = 0;
}
if (state.minutes === 60) {
state.hours++;
state.minutes = 0;
}
if (state.hours === 24) {
state.hours = 0;
}
emitter.emit('render');
});
return state;
}
let interval;
const hook = {
create() {
interval = setInterval(() => emitter.emit('clock:update'), 1000);
},
destroy(vnode, removeCallback) {
clearInterval(interval);
removeCallback();
}
};
function displayDigit(digit) {
if (digit < 10) {
return `0${digit}`;
}
return digit;
}
function view({ state }) {
return h('h1', { hook }, [
h('span.hours', displayDigit(state.hours)),
':',
h('span.minutes', displayDigit(state.minutes)),
':',
h('span.seconds', displayDigit(state.seconds))
]);
}
return {
view,
store
};
}
DEMO
- Each component has their own emitter and is passed as a destructuring argument for the component.
- In the store function, we define the
event handlers
that can do mutable updates to our state. - There is no magic in snabbmitt, update your state doesn't mean that the component will be rendered again, you must force the render explicit using:
emitter.emit('render')
- The hook implementation of snabbdom is great and we use it to start/stop the timer of each component.
and that's it!
Making the clock app a fractal component for an App of clocks?
Well, I say that the App function is a component, why don't we called Clock? and then...
function Clock({ emitter, props }) {
function store() {
const state = {
hours: props.time ? props.time[0] : 0,
minutes: props.time ? props.time[1] : 0,
seconds: props.time ? props.time[2] : 0
};
emitter.on('clock:update', () => {
state.seconds++;
if (state.seconds === 60) {
state.minutes++;
state.seconds = 0;
}
if (state.minutes === 60) {
state.hours++;
state.minutes = 0;
}
if (state.hours === 24) {
state.hours = 0;
}
emitter.emit('render');
});
return state;
}
let interval;
const hook = {
create() {
interval = setInterval(() => emitter.emit('clock:update'), 1000);
},
destroy(vnode, removeCallback) {
clearInterval(interval);
removeCallback();
}
};
function displayDigit(digit) {
if (digit < 10) {
return `0${digit}`;
}
return digit;
}
function view({ state, props }) {
return h('h1', { hook }, [
props.name,
' => ',
h('span.hours', displayDigit(state.hours)),
':',
h('span.minutes', displayDigit(state.minutes)),
':',
h('span.seconds', displayDigit(state.seconds))
]);
}
return {
view,
store
};
}
function App() {
function view() {
return h('div', [
component(Clock, { name: 'Clock 1' }),
component(Clock, { name: 'Clock 2', time: [5 ,20, 16] }),
component(Clock, { name: 'Clock 3', time: [23, 59, 40] })
]);
}
return view;
}
DEMO
- The snabbmitt instance returns a
component function factory
too. With this function, you can create stateful components in your app. - Your component can receive a props argument object from his parent and use it either when the component is creating or the view runs.