【问题标题】:what is the reason of decorators not working? (mobx)装饰器不工作的原因是什么? (暴民)
【发布时间】: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) 一切正常。

【问题讨论】:

标签: reactjs decorator mobx


【解决方案1】:

从 MobX 6 开始,您必须在构造函数中调用 makeObservable/makeAutoObservable

【讨论】:

    猜你喜欢
    • 2015-08-02
    • 2020-07-23
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 2022-08-21
    • 1970-01-01
    • 2021-05-10
    • 2017-04-10
    相关资源
    最近更新 更多