react and-rx

15
React と RX

Upload: -

Post on 13-Apr-2017

238 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: React and-rx

React と RX

Page 2: React and-rx

名前 : @brn ( 青野健利 )職業 : フロントエンドエンジニア・ネイティブエンジニア会社 : Cyberagent アドテクスタジオ RightSegment ・ AI Messengerブログ : http://brn-log.hatenablog.com/

Page 3: React and-rx

アーキテクチャの問題

Redux はよくできている。が、もっと検討してみよう。

うん? Cyclejs ?なんだこれ?

Page 4: React and-rx

アーキテクチャの問題

Page 5: React and-rx

アーキテクチャの問題

よし、パクろう。けど JSX 使いたい。よし、作ろう。

Page 6: React and-rx

MVI アーキテクチャを取り入れる

Page 7: React and-rx

MVI アーキテクチャを取り入れる

Page 8: React and-rx

React + RX

Cyclejs をパクって、 Rxjs を取り入れたけど、Rxjs の Subscribe でレンダリングするのは嫌ー

Props に Observable を渡したらよしなにしてほしい…

よし、作ろう…

Page 9: React and-rx

react-mvi

import { Tags as T} from '@react-mvi/core';

class Component extends React.Component { render() { <T.Div> <h1>{this.props.text.map(v => v + ' World')}</h1> </T.Div> }}

Page 10: React and-rx

react-mvi

内部では、 Observable を subscribe して VirtualDOM を書き換えています。変更が起きた所のみをミュータブルに変更するので、高速です。shouldComponentUpdate 必要ないです。

結果通常の React では不可能な部分更新に対応できました。

Page 11: React and-rx

react-mvi

なんか MVI の Model になんでも入ってて嫌だから分割しよう。そんで、分割したクラスはもちろん DI したい。DI コンテナも作るか。

Page 12: React and-rx

react-mvi

import { createModule, Injector, inject} from '@react-mvi/core';import { MyService} from './myservice';

class MyComponent { @inject() private myService; ...}

const module = createModule(config => { config.bind('myService').to(MyService).asSingleton();});

const injector = new Injector([module]);const myComponent = injector.inject(MyComponent);

Page 13: React and-rx

react-mvi

ちゃんと React コンポーネントでも動きます!

Page 14: React and-rx

react-mviimport React from 'react'import { createModule, Injector, inject, run} from '@react-mvi/core';import { MyService} from './myservice';

class MyComponent extends React.Component { @inject() private myService; ...}

const module = createModule(config => { config.bind('myService').to(MyService).asSingleton();});

const injector = new Injector([module]);run({component: MyComponent, injector}); // SAME AS ReactDOM.render

Page 15: React and-rx

react-mvi

https://github.com/brn/react-mvi

私はスターが好きです。I like github star.