Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { decorate, observable, action } from 'mobx';
- import React from 'react';
- class TheTimer {
- currentTick = 0; // In seconds since start
- tick = () => {
- this.currentTick = Math.floor(performance.now() / 1000);
- }
- }
- decorate(TheTimer, {
- currentTick: observable,
- tick: action
- });
- // Bare store
- export const timerStore = new TheTimer();
- // Store as React context
- export const TimerStoreContext = React.createContext(timerStore);
- // This makes the timer go
- setInterval(timerStore.tick, 100);
- import React from 'react';
- import { configure } from 'mobx';
- import { observer } from 'mobx-react';
- import { timerStore } from './store';
- configure({ enforceActions: 'observed' });
- export default const App = observer(() => {
- return (
- <p>{timerStore.currentTick}</p>
- );
- });
- import React from 'react';
- import { configure } from 'mobx';
- import { observer } from 'mobx-react';
- import { TimerStoreContext } from './store';
- configure({ enforceActions: 'observed' });
- export default const App = observer(() => {
- const timerStore = React.useContext(TimerStoreContext);
- return (
- <p>{timerStore.currentTick}</p>
- );
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement