|
1 | | -import React, { Component, PropTypes } from 'react'; |
| 1 | +import React from 'react'; |
2 | 2 | import {render} from 'react-dom'; |
3 | | -import most from 'most'; |
4 | | -import Most from '../../../lib/react-most' |
5 | | -import {connect} from '../../../lib/react-most' |
6 | | -const App = React.createClass({ |
| 3 | +import Most, {connect} from '../../../lib/react-most' |
7 | 4 |
|
8 | | - getInitialState(){ |
9 | | - return {show: false} |
10 | | - }, |
11 | | - componentDidMount() { |
12 | | - this.props.actions.add(); |
13 | | - this.props.actions.add(); |
14 | | - this.props.actions.add(); |
15 | | - setTimeout(() => this.setState({ |
16 | | - show: true |
17 | | - }, () => { |
18 | | - this.props.actions.add(); |
19 | | - this.props.actions.add(); |
20 | | - this.props.actions.add(); |
21 | | - }), 2000); |
22 | | - }, |
23 | | - render(){ |
24 | | - return ( |
| 5 | +const CounterView = props => ( |
25 | 6 | <div> |
26 | | - <RCount /> |
27 | | - {this.state.show ? <RCount/> : null} |
28 | | - <button onClick={this.props.actions.add}>+</button> |
| 7 | + <button onClick={props.actions.dec}-</button> |
| 8 | + <span>{props.count}</span> |
| 9 | + <button onClick={props.actions.inc}>+</button> |
29 | 10 | </div> |
30 | 11 | ) |
31 | 12 | } |
32 | 13 | }) |
33 | 14 |
|
34 | | -const Count = (props) => { |
35 | | - return <div>{props.count}</div> |
36 | | -} |
37 | | -const RCount = connect((intent$)=>{ |
38 | | - return { |
39 | | - countSink$: intent$ |
40 | | - .filter(x => x.type === 'add') |
41 | | - .map(intent => state => ({count: (state.count||0) + 1})), |
42 | | - } |
43 | | -})(Count) |
| 15 | +CounterView.defaultProps = {count: 0}; |
44 | 16 |
|
45 | | -const RApp = connect((intent$) => { |
46 | | - return { |
47 | | - add: _=>({type: 'add'}), |
48 | | - } |
49 | | -})(App) |
| 17 | +const reactify = connect((intent$)=>{ |
| 18 | + return { |
| 19 | + sink$: intent$.map(intent => { |
| 20 | + switch(intent.type) { |
| 21 | + case 'inc': |
| 22 | + return state => ({count: state.count+1}); |
| 23 | + case 'dec': |
| 24 | + return state => ({count: state.count-1}); |
| 25 | + default: |
| 26 | + return _ => _; |
| 27 | + } |
| 28 | + }), |
| 29 | + inc: () => ({type: 'inc'}), |
| 30 | + dec: () => ({type: 'dec'}), |
| 31 | + } |
| 32 | +}) |
| 33 | + |
| 34 | +const Counter = reactify(CounterView) |
50 | 35 |
|
51 | 36 | render( |
52 | 37 | <Most> |
53 | | - <RApp/> |
| 38 | + <Counter /> |
54 | 39 | </Most> |
55 | 40 | , document.getElementById('app')); |
0 commit comments