一共有兩個套件: react
優點,就是因爲使用了 proxy,支持跟蹤不存在的變量,好比:git
import { observe, observable } from 'dob'
const dynamicObj = observable({
a: 1
})
observe(() => {
console.log('dynamicObj.b change to', dynamicObj.b)
})
// 如今要修改 b 了,b 以前不存在
dynamicObj.b = 2 // dynamicObj.b change to 2複製代碼
很方便吧,實現了 mobx 求之不得的夙願,至今爲止但願實現的 dob-react 已經被完美實現了。github
和 mobx-react 別無二致,優化點在於,再也不區分 observer 與 inject,全部綁定與注入集成在一個裝飾器(由於有依賴追蹤,因此全量注入沒有性能問題,這很 DX)typescript
import { Provider, Connect } from 'dob-react'
@Connect
class App extends React.Component <any, any> {
render() {
return (
<span>{this.props.store.name}</span>
)
}
}
ReactDOM.render(
<Provider store={{ name: 'bob' }}>
<App />
</Provider>
, document.getElementById('react-dom'))複製代碼
第二個優化點,在於不須要手動指定 @Observerable,全部變量自動開啓跟蹤。bash
yarn add dob dependency-inject --save複製代碼
store.ts
:app
import { inject, Container } from 'dependency-inject'
import { Action } from 'dob'
export class Store {
name = 'bob'
}
export class Action {
@inject(Store) store: Store
@Action setName (name: string) {
this.store.name = name
}
}
const container = new Container()
container.set(Store, new Store())
container.set(Action, new Action())
export { container }複製代碼
app.ts
:dom
import { Provider, Connect } from 'dob-react'
import { Store, Action, container } from './store'
@Connect
class App extends React.Component <any, any> {
componentWillMount () {
this.props.action.setName('nick')
}
render() {
return (
<span>{this.props.name}</span>
)
}
}
ReactDOM.render(
<Provider store={container.get(Store)} action={container.get(Action)}>
<App />
</Provider>
, document.getElementById('react-dom'))複製代碼