Skip to content

Commit 7180b8b

Browse files
committed
rewrite counter example
1 parent 5eed7f8 commit 7180b8b

File tree

1 file changed

+26
-41
lines changed

1 file changed

+26
-41
lines changed

examples/counter/src/app.jsx

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,40 @@
1-
import React, { Component, PropTypes } from 'react';
1+
import React from 'react';
22
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'
74

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 => (
256
<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>
2910
</div>
3011
)
3112
}
3213
})
3314

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};
4416

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)
5035

5136
render(
5237
<Most>
53-
<RApp/>
38+
<Counter />
5439
</Most>
5540
, document.getElementById('app'));

0 commit comments

Comments
 (0)