Guest User

Untitled

a guest
Apr 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. //註冊dispatcher 設定Action要指向哪一個Store
  2. dispatcher.register(function(payload) {
  3. switch (payload.actionType) {
  4. case 'ISSUE_UPDATE':
  5. //dispatcher 把 aciton 的 payload 丟給 store
  6. store.emitMessage(payload.data);
  7. break;
  8. }
  9. return true;
  10. });
  11.  
  12. //宣告 Actions ,當 aciton 有動作時呼叫 dispatcher
  13. const Actions = {
  14. create: payload => {
  15. var action = {
  16. actionType: 'ISSUE_UPDATE',
  17. data: payload.data,
  18. };
  19. dispatcher.handleViewAction(action);
  20. },
  21. };
  22.  
  23. export default class App extends React.Component {
  24. constructor(props, context) {
  25. super(props, context);
  26. //State ,當有更動時render會重新執行
  27. this.state = {
  28. issues: [],
  29. };
  30.  
  31. this.handleOnNewUpdate = this.handleOnNewUpdate.bind(this);
  32. }
  33.  
  34. componentDidMount() {
  35. //設定Store 的Listener監聽器, 隨時接收dispatcher
  36. store.addListener(this.handleOnNewUpdate);
  37. this._tid = setInterval(function() {
  38. axios.get(api).then(response => {
  39. //取得資料時, 建立一個Action
  40. Actions.create({
  41. data: response.data,
  42. });
  43.  
  44. });
  45. }, 3000);
  46. }
  47.  
  48. handleOnNewUpdate(data) {
  49. this.setState({
  50. issues: data,
  51. });
  52. }
  53.  
  54. componentWillUnmount() {
  55. clearInterval(this._tid);
  56. }
  57.  
  58. render() {
  59. return (
  60. <View style={styles.container}>
  61. <FlatList
  62. numColumns={2}
  63. ListHeaderComponent={this.fluxDemo}
  64. ListFooterComponent={this.fluxDemo}
  65. data={this.state.issues}
  66. keyExtractor={item => item.id}
  67. renderItem={({ item }) => (
  68. <View style={styles.item}>
  69. <Text>{item.title}</Text>
  70. <Text>{item.body}</Text>
  71. </View>
  72. )}
  73. />
  74. </View>
  75. );
  76. }
  77.  
  78. }
Add Comment
Please, Sign In to add comment