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中,这一功能得到了 ...
随机推荐
- ElasticSearch 5.4 安装
1. 前期准备 环境准备 IP地址 操作系统 内存 192.168.1.10 centos 7 16 192.168.1.11 centos 7 16 192.168.1.12 centos ...
- 【Coursera】Security Introduction -Ninth Week(2)
对于公钥系统,我们现在已经有了保证它 Confidentially 的一种方法:SSL.SSL利用了公钥的概念. 那么 who we are talking to? Integrity Certifi ...
- 【Coursera】Security Introduction -Eighth Week(1)
Security Introduction People With Bad Intent 今天,Bob 向 Alice 发送了一条 "Hello,Allice!" 的信息,他们希望 ...
- 01_Kafka概述
1.Kafka背景 * LinkedIn开发,2011年成为Apache的一个开源项目* 2012年,成为Apache的一个顶级项目* 基于java和Scala编写, 基于发布-订阅模型的消息系统(离 ...
- Java DateUtil,日期整理
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...
- Could NOT find SDL (missing: SDL_LIBRARY SDL_INCLUDE_DIR)
sudo apt-get install libsdl-dev或 sudo apt-get install libsdl1.2-dev
- Linux中CentOS6.5 64位 系统下安装docker步骤
CentOS6.5 64位 (docker目前仅支持64位)内核必须在3.10及以上 1. uname -r 查看内核版本 2. 升级内核到3.10版本(带aufs模块) cd /etc ...
- Codeforces 96C - Hockey
96C - Hockey 字符串处理 代码: #include<bits/stdc++.h> using namespace std; #define ll long long ; con ...
- Java 历史
James Gosling 最初开始 Java 语言项目是在 1991 年的 7 月.Java 被用在他的许多 set-top box 工程中.这个语言最开始的时候被称为 Oka,这个是因为 Jame ...
- nyoj-310-河南省第四届省赛题目-二分+dinic
SECRET 时间限制:3000 ms | 内存限制:65535 KB 难度:6 描述 Dr.Kong is constructing a new machine and wishes to ...