【发布时间】:2021-11-23 16:59:48
【问题描述】:
如果没有 makeAutoObservable(this) 函数,装饰器将无法工作。 例子: 组件
import { observer } from 'mobx-react-lite'
import useStore from 'hooks/useStore'
const Counter = props => {
const { counterStore } = useStore()
const { countValue } = counterStore
console.log(counterStore)
return (
<div>
<button>-</button>
<p>{countValue} значение</p>
<button onClick={counterStore.incrementCountValue}>+</button>
</div>
)
}
export default observer(Counter)
商店
import { action, observable } from 'mobx'
export default class CounterStore {
@observable countValue = 0
@action incrementCountValue = () => {
this.countValue = this.countValue + 1
}
}
如果使用 makeAutoObservable(this) 一切正常。
【问题讨论】: