7,765
社区成员
发帖
与我相关
我的任务
分享
const Countdown=({count,tick,reset})=>{
if(count){
setTimeout(()=>tick(),1000)
}
return (count)?
<h1>{count}</h1>:
<div onlick={()=>reset(10)}>
<span>CELEBRATE!!!</span>
</div>
}
const countdownActions = dispatcher=>({
tick(currentCount) {
dispatcher.handleAction({type:'TICK'})
},
reset(count) {
dispatcher.handleAction({
type: 'Reset',
count
})
}
})
import Dispatcher from 'flux'
class CountdownDispatcher extends Dispatcher{
handleAction(action){
console.log('dispatching action:',action);
this.dispatch({
source:'VIEW_ACTION',
action
})
}
}
import {EventEmitter} from 'events'
class CountdownStore extends EventEmitter{
constructor(count=5,dispatcher){
super()
this._count=count
this.dispatcherIndex=dispatcher.register(this.dispatch.bind(this))
}
}
get count(){
return this._count
}
dispatch(payload){
const {type,count}=payload.action
switch(type){
case "TICK":
this._count=this._count-1
this.emit("TICK",this._count)
return true;
case "RESET":
this._count=count
this.emit("RESET",this._count)
return true
}
}
const appDispatcher = new CountdownDispatcher()
const actions = countdownActions(appDispatcher)
const store = new CountdownStore(10, appDispatcher)
const render = count=>ReactDOM.render(
<Countdown count={count} {...actions}/>,
document.getElementById('react-container')
)
store.on('TICK',()=>render(store.count))
store.on('RESET',()=>render(store.count))
render(store.count)