mobx

本文最后更新于:2022年4月20日 下午

MobX

数据检测

autorun

autorun(effect: (reaction) => void)
  • 初始化执行一次
  • 只能访问当前状态

    reaction

    reaction(() => value, (value, previousValue, reaction) => { sideEffect }, options?)
  • 初始化不执行
  • 能访问之前状态

Tips

强制绑定action的this指向

  • class中
    class CounterStore{
      count
      constructor(count){
        makeObservable(this,{
          count:observable,
          increase:action.bound
        })
        this.count = count
      }
      increase(){
        this.count++
      }
    }
    
    // or 箭头函数
    	increase = ()=> {
    		this.count++;
    	}
  • function中
    function createCounterStore(count) {
    	return makeAutoObservable({
    		count,
    		increase(){
    			this.count++;
    		},
    	},{}, { autoBind: true });
    }

mobx
http://yoursite.com/2022/04/19/react_mobx/
作者
tatekii
发布于
2022年4月19日
许可协议