最近新人加入了项目,遇到了一个很奇怪的问题。mobx observable 属性,onChange的时候就是页面不会刷新。

试来试去,就是不知道什么原因,后来其他同事查到是因为componentWillMount写成了箭头函数,真的是防不胜防啊。

错误代码如下,componentWillMount的箭头函数去掉就妥妥的对的

whyRun函数运行的结果如下,显示的意思是没有在收集依赖的函数中运行

  whyRun() can only be used if a derivation is active, or by passing an computed value / reaction explicitly.

  If you invoked whyRun from inside a computation;

  the computation is currently suspended but re-evaluating because somebody requested its value.

但是为什么呢??那只能看看mobx-react源代码了,下面observer方法的截图,删除了几行,通过mixinLifecycleEvents对react组件的生命周期函数进行了扩展

下面是patch方法的实现,是不是有种切面编程,函数after,before的影子

具体"componentWillMount", "componentDidMount", "componentWillUnmount", "componentDidUpdate"方法进行了哪些扩展,不是今天的重点(我也不是很清楚)。

componentWillMount的扩展比较重要,

主要做了下面几件事

  props,state 通过 Atom 对象封装

  定义方法initialRender,创建 Reaction对象(当可监听依赖变化的时候,触发组件forceUpdate),跟踪收集 原render函数 里面的依赖

  用initialRender覆盖原render函数

当组件componentWillMount函数,使用了箭头函数,就会在组件 constructor 方法中,bind this并直接覆盖方法,使得mobx-react都白干了,

所以whyRun也会提示没有在收集跟踪方法中运行了。

在代码中箭头函数还是不能滥用啊~~

最新文章

  1. [PHP源码阅读]explode和implode函数
  2. LeetCode Permutations (全排列)
  3. hdu 4252 A Famous City
  4. JAVA之数据溢出
  5. BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路
  6. GridView点击空白地方事件扩展
  7. java单词表01
  8. HDU 1010 Temper of the bone(深搜+剪枝)
  9. oracle数据库命令-持续更新
  10. ajax和json
  11. 菜鸟学IT-分布式版本控制系统Git的安装与使用
  12. MYSQL的学习
  13. 小程序如何传数组数据到vs后台中
  14. Git的安装和使用
  15. HDU2167(SummerTrainingDay02-D 状态压缩dp)
  16. git(常用命令)思维导图...
  17. NodeJS之 Express框架 app.use(express.static)
  18. Enable Scribble,Enable Guard Edges,Enable Guard Malloc,Zombie Objects
  19. 生成器(generator)和迭代(iterable , iterator, iteration)
  20. html5兼容问题

热门文章

  1. spring cloud config的bootstrap.yml与application.proterties的区别
  2. tomcat7 access log设置
  3. linux部署j2eeweb工程涉及到的指令
  4. 其他:OI竞赛中的文件操作
  5. Git 常见工作流
  6. 【BZOJ2693】jzptab [莫比乌斯反演]
  7. 51nod 1486 大大走格子——dp
  8. 【BZOJ】1588: [HNOI2002]营业额统计
  9. bootstrap 弹窗 数据清除
  10. Python面向对象学习2(面向对象的语法和特性,待更新)