LoginSignup
1
1

More than 5 years have passed since last update.

Getting StartedのサンプルをMVIで書く

Posted at

Getting StartedのサンプルをMVIで書くと多分こんな感じ。

import Cycle from "@cycle/core";
import {makeDOMDriver, hJSX} from "@cycle/dom";

function intent(DOM) {
  return {
    toggleSwitch$: DOM.select("input").events("change")
      .map(ev => ev.target.checked)
  };
}

function model(actions) {
  return actions.toggleSwitch$.startWith(false)
    .map(isOn => ({isOn}));
}

function view(state$) {
  return state$.map(({isOn}) =>
    <div>
      <input type="checkbox" /> Toggle me
      <p>{isOn ? "ON" : "OFF"}</p>
    </div>
  );
}

function main(drivers) {
  return {
    DOM: view(model(intent(drivers.DOM)))
  };
}

Cycle.run(main, {
  DOM: makeDOMDriver("main")
});

ちなみにJSXをbabelで変換する場合は、pluginの場合["transform-react-jsx", { "pragma": "hJSX" }]、preset reactを使う場合はファイル冒頭にコメント/** @jsx hJSX */を入れる必要がある。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1