Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import { decorate, observable, action } from 'mobx';
  2. import React from 'react';
  3.  
  4. class TheTimer {
  5. currentTick = 0; // In seconds since start
  6.  
  7. tick = () => {
  8. this.currentTick = Math.floor(performance.now() / 1000);
  9. }
  10. }
  11.  
  12. decorate(TheTimer, {
  13. currentTick: observable,
  14. tick: action
  15. });
  16.  
  17. // Bare store
  18. export const timerStore = new TheTimer();
  19. // Store as React context
  20. export const TimerStoreContext = React.createContext(timerStore);
  21.  
  22. // This makes the timer go
  23. setInterval(timerStore.tick, 100);
  24.  
  25. import React from 'react';
  26. import { configure } from 'mobx';
  27. import { observer } from 'mobx-react';
  28.  
  29. import { timerStore } from './store';
  30.  
  31. configure({ enforceActions: 'observed' });
  32.  
  33. export default const App = observer(() => {
  34. return (
  35. <p>{timerStore.currentTick}</p>
  36. );
  37. });
  38.  
  39. import React from 'react';
  40. import { configure } from 'mobx';
  41. import { observer } from 'mobx-react';
  42.  
  43. import { TimerStoreContext } from './store';
  44.  
  45. configure({ enforceActions: 'observed' });
  46.  
  47. export default const App = observer(() => {
  48. const timerStore = React.useContext(TimerStoreContext);
  49.  
  50. return (
  51. <p>{timerStore.currentTick}</p>
  52. );
  53. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement