剧场模式
首页前端React轻松学 MobX 视频教程

轻松学 MobX 视频教程 #4 通过 mobx 学习观察者模式

海外散仙厉飞雨 · 真仙发布于
1

https://github.com/mobxjs/mobx-react

源码:https://github.com/hfpp2012/hello-mobx

src/stores/BirdStore.js

import { observable, autorun } from 'mobx';

class BirdStore {
  // object array map
  @observable birds = [];
}

const store = window.store = new BirdStore();

export default store;

autorun(() => {
  console.log('print');
  console.log(store.birds);
})

src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import BirdStore from './stores/BirdStore';

ReactDOM.render(<App />, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
0 条回复
暂无回复~~

© 汕尾市求知科技有限公司 | 粤ICP备19038915号 | 在线学员:27

Top