componentWillMount有用过吗?
初始化数据
在render中需要用到的数据,比如权限设置
componentDidMount
在render之后并不会立即被调用,而是等到所有的子组件都渲染完成后才会被调用,componentWillMount既可以在服务器端被调用,也可以在浏览器端被调用,而componentDidMount只能在浏览器端被调用
componentWillMount不执行ajax的原因?
- react16版本之后采用了fiber架构,只有componenntDidMount是被确定执行一次,而componentWillMount有可能被执行多次
- 服务器端渲染,componentWillMount会被执行两次
componentWillUnmount中做的操作?
- 移除定时器:定时器是由浏览器单独的线程管理的,页面销毁还存在
- netWorking:如果页面跳转了,设置一个变量的值为false,直接return,不在进行setState
- componentDidMount中监听订阅行为
js中常见的事件模式
- 单例模式
单例模式的实现旨在保证一个特定类只有一个实例,第二次使用同一个类创建的对象应该是一样的,地址都相同。
判断是否已经有实例了;闭包来实现;
惰性单例:通过创建一个变量和一个函数来实现,让变量等于这个函数的执行结果,返回一个函数,函数的返回值是这个变量或者变量等于这个函数的执行结果。
function test () {
if (typeOf test.instance === "Object") {
return test.instance;
}
test.instance = this;
return this;
}
function test () {
var instance = this;
test = function() {
return instance;
}
}
- 工厂模式
- 适配模式
- 外观模式