Created
April 30, 2020 10:35
-
-
Save zsytssk/8f32537e5e6c206af00500ee36d19112 to your computer and use it in GitHub Desktop.
react hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EventCom } from '../eventCom'; | |
import { useState, useEffect } from 'react'; | |
const StateEvent = { | |
ChangeCount: 'change_count', | |
ChangeTest: 'change_test', | |
}; | |
class StateModel extends EventCom { | |
public count = 0; | |
public test = { x: 1 }; | |
public setCount = () => { | |
this.count++; | |
this.emit(StateEvent.ChangeCount); | |
}; | |
public setTest = () => { | |
this.test.x++; | |
this.emit(StateEvent.ChangeTest); | |
}; | |
} | |
let state = new StateModel(); | |
export function getState() { | |
const [_state, setState] = useState(state); | |
const [changeIndex, setChangeIndex] = useState(0); | |
useEffect(() => { | |
const fn = () => { | |
setState(state); | |
setChangeIndex(changeIndex + 1); | |
}; | |
state.on(StateEvent.ChangeCount, fn); | |
state.on(StateEvent.ChangeTest, fn); | |
return () => { | |
state.off(StateEvent.ChangeCount, fn); | |
state.off(StateEvent.ChangeTest, fn); | |
}; | |
}, [changeIndex]); | |
return [_state] as [StateModel]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment