Meiosis Documentation

< Previous | Next > | Table of Contents

07 - Views

In the previous lessons, we set up the Meiosis pattern with a temperature example. In this section, we'll wire this up to three different view libraries:

Actions

We had an actions object to update the state. To simplify, let's just have the increment action:

const actions = {
  increment: (cell, amount) =>
    cell.update({ value: (x) => x + amount })
};

This example uses Mergerino, but of course you can also use function patches if you prefer.

The view can call actions.increment to trigger updates.

Cells

Next, remember that in the previous section, we set up a stream of cells:

const createCell = (state) => ({ state, update });
const cells = states.map(createCell);

We can call cells() to get the current cell, or we can use cells.map(cell => ...) to render the view on every update.

View

The view is a function that gets the current cell as a parameter, from which the current state is available as cell.state. In the view, event handlers (onclick or onClick in the examples below) call actions, passing cell and any additional parameters.

Mithril

Let's see how we can wire up Meiosis to Mithril.

First, we can use Mithril Stream as a stream library. For our purposes, it works just like flyd. The only difference is that you call m.stream() instead of flyd.stream(), and m.stream.scan instead of flyd.scan.

Then, we use m.mount and pass the current cell to the view by calling cells():

m.mount(document.getElementById('app'), {
  view: () => app.view(cells())
});

The complete example is below.

With Mithril's auto-redraw system, the view is automatically re-rendered after user interaction. If our application updates the state outside of Mithril's auto-redraw scope (see When Mithril does not redraw), we can re-render the view on state updates simply by using map on our stream of cells and calling m.redraw():

cells.map(() => m.redraw());

Preact

To wire up Meiosis to Preact, we use cells.map to render the view whenever the state changes. We call preact.render to render the view:

const element = document.getElementById('app');
cells.map((cell) => {
  preact.render(app.view(cell), element);
});

You can see the complete example below.

React

Wiring Meiosis to React is essentially the same as with Preact, the only difference is that we use ReactDOM.render to render the view:

const element = document.getElementById('app');
cells.map((cell) => {
  ReactDOM.render(app.view(cell), element);
});

You can see the complete example below.

Starting with React 18, ReactDOM.render(view, element) has been deprecated in favor of root = createRoot(element) and root.render(view). This doesn't change how to wire up Meiosis aside from adjusting how to use the React API.

Conclusion

In this section, we saw how to wire up Meiosis to a view library. Our base Meiosis pattern is complete!

The remainder of this documentation covers additional functionalities that we can add to Meiosis, starting with Services.

< Previous | Next > | Table of Contents


Meiosis is developed by foxdonut (Twitter / GitHub) and is released under the MIT license.