anu - component
import { extend, isFn, options, clearArray, noop } from "./util";
import { CurrentOwner } from "./createElement";
/**
*组件的基类
*
* @param {any} props
* @param {any} context
*/
var mountOrder = 1;
export function Component(props, context) {
//防止用户在构造器生成JSX
CurrentOwner.cur = this;
this.__mountOrder = mountOrder++;
this.context = context;
this.props = props;
this.refs = {};
this.state = null;
this.__pendingCallbacks = [];
this.__pendingStates = [];
this.__current = noop;
/*
* this.__hydrating = true 表示组件正在根据虚拟DOM合成真实DOM
* this.__renderInNextCycle = true 表示组件需要在下一周期重新渲染
* this.__forceUpdate = true 表示会无视shouldComponentUpdate的结果
*/
}
Component.prototype = {
constructor: Component,//必须重写constructor,防止别人在子类中使用Object.getPrototypeOf时找不到正确的基类
replaceState() {
console.warn("此方法末实现"); // eslint-disable-line
},
setState(state, cb) {
debounceSetState(this, state, cb);
},
isMounted() {
return !!this.__dom;
},
forceUpdate(cb) {
debounceSetState(this, true, cb);
},
__mergeStates: function (props, context) {
var n = this.__pendingStates.length;
if (n === 0) {
return this.state;
}
var states = clearArray(this.__pendingStates);
var nextState = extend({}, this.state);
for (var i = 0; i < n; i++) {
var partial = states[i];
extend(nextState, isFn(partial)
? partial.call(this, nextState, props, context)
: partial);
}
return nextState;
},
render() { }
};
function debounceSetState(a, b, c) {
if (a.__didUpdate) {//如果用户在componentDidUpdate中使用setState,要防止其卡死
setTimeout(function () {
a.__didUpdate = false;
setStateImpl.call(a, b, c);
}, 300);
return;
}
setStateImpl.call(a, b, c);
}
function setStateImpl(state, cb) {
if (isFn(cb)) {
this
.__pendingCallbacks
.push(cb);
}
let hasDOM = this.__dom;
if (state === true) {//forceUpdate
this.__forceUpdate = true;
} else {//setState
this
.__pendingStates
.push(state);
}
if (!hasDOM) { //组件挂载期
//componentWillUpdate中的setState/forceUpdate应该被忽略
if (this.__hydrating) {
//在挂载过程中,子组件在componentWillReceiveProps里调用父组件的setState,延迟到下一周期更新
this.__renderInNextCycle = true;
}
} else { //组件更新期
if (this.__receiving) {
//componentWillReceiveProps中的setState/forceUpdate应该被忽略
return;
}
this.__renderInNextCycle = true;
if (options.async) {
//在事件句柄中执行setState会进行合并
options.enqueueUpdate(this);
return;
}
if (this.__hydrating) {
// 在componentDidMount里调用自己的setState,延迟到下一周期更新
// 在更新过程中, 子组件在componentWillReceiveProps里调用父组件的setState,延迟到下一周期更新
return;
}
// 不在生命周期钩子内执行setState
options.flushBatchedUpdates([this]);
}
}
anu - component的更多相关文章
- 发布高性能迷你React框架anu
anu, 读作[安努],原意为苏美尔的主神. anu是我继avalon之后又一个新框架(github仓库为https://github.com/RubyLouvre/anu, 欢迎加星与试用) 此框架 ...
- 利用React/anu编写一个弹出层
本文将一步步介绍如何使用React或anu创建 一个弹出层. React时代,代码都是要经过编译的,我们很多时间都耗在babel与webpack上.因此本文也介绍如何玩webpack与babel. 我 ...
- 高性能迷你React框架anu在低版本IE的实践
理想是丰满的,现实是骨感的,react早期的版本虽然号称支持IE8,但是页面总会不自觉切换到奇异模式下,导致报错.因此必须让react连IE6,7都支持,这才是最安全.但React本身并不支持IE6, ...
- anu小程序快速入门
众所周知,微信推出小程序以来,可谓火遍大江南北,就像当前互联网兴起时,大家忙着抢域名与开私人博客一样.小程序之所以这么火,是因为微信拥有庞大的用户量,并且腾讯帮你搞定后台问题及众多功能问题(如分享,支 ...
- openfire的组件(Component)开发
在之前的文章<Openfire阶段实践总结>中提到过一种openfire的扩展模式Compoent.本文将主要探讨对这种模式的应用与开发方法. 内部与外部组件介绍 在openfire中的许 ...
- salesforce 零基础学习(六十一)apex:component简单使用以及图片轮转播放的实现
有的时候,我们项目有可能有类似需求:做一个简单的图像轮转播放功能,不同的VF页面调用可以显示不同的图片以及不同的图片描述.这种情况,如果在每个页面单独处理相关的图像轮转播放则显得代码特别冗余,此种情况 ...
- angular2 service component
[component 需要通过 service 提供的接口 得到一些数据.这是最佳实践.] [由于 有 component 和 service 两个语义,所以出现了下面两种办法] 一,[service ...
- knockoutjs如何动态加载外部的file作为component中的template数据源
玩过knockoutjs的都知道,有一个强大的功能叫做component,而这个component有个牛逼的地方就是拥有自己的viewmodel和template, 比如下面这样: ko.compon ...
- 解读ASP.NET 5 & MVC6系列(14):View Component
在之前的MVC中,我们经常需要类似一种小部件的功能,通常我们都是使用Partial View来实现,因为MVC中没有类似Web Forms中的WebControl的功能.但在MVC6中,这一功能得到了 ...
随机推荐
- EasyUI ---- draggable可拖动的用法
<link href="~/Scripts/easyui1.5/themes/default/easyui.css" rel="stylesheet" / ...
- Axure RP 8.0 Licence
新版本:(比如 Axure RP 8.0.0 3319)Licensee:米 业成 (STUDENT)Key:nFmqBBvEqdvbiUjy8NZiyWiRSg3yO+PtZ8c9wdwxWse4W ...
- 获取客户端真实ip地址(无视代理)
/// <summary> /// 获取客户端IP地址(无视代理) /// </summary> /// <returns>若失败则返回回送地址</retur ...
- MySQL会创建临时表的几种情况
1.UNION查询: 2.用到TEMPTABLE算法或者是UNION查询中的视图: 3.ORDER BY和GROUP BY的子句不一样时: 4.表连接中,ORDER BY的列不是驱动表中的:(指定了联 ...
- resource not found :rgbd_launch
放到src下,再次编译catkin_make git https://github.com/ros-drivers/rgbd_launch.git
- Error updating database. Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'as3'
执行更新时的出错信息 Whitelabel Error Page This application has no explicit mapping for /error, so you are see ...
- Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks论文理解
一.创新点和解决的问题 创新点 设计Region Proposal Networks[RPN],利用CNN卷积操作后的特征图生成region proposals,代替了Selective Search ...
- 【Robot Framework 项目实战 01】使用 RequestsLibrary 进行接口测试
写在前面 本文我们一起来学习如何使用Robot Framework 的RequestsLibrary库,涉及POST.GET接口测试,RF用例分层封装设计等内容. 接口 接口测试是我们最常见的测试类型 ...
- Python 爬虫-BeautifulSoup
2017-07-26 10:10:11 Beautiful Soup可以解析html 和 xml 格式的文件. Beautiful Soup库是解析.遍历.维护“标签树”的功能库.使用Beautifu ...
- Linq 使用skip和take分页
static int Main(string[] args) { //每页条数 const int pageSize = 2; //页码 0就是第一条数据 int pageNum = 0; strin ...