Javascript 何时调用'React.createClass({})(')
我正在挖掘Javascript 何时调用'React.createClass({})('),javascript,reactjs,Javascript,Reactjs,我正在挖掘React.js源代码,这就是我目前试图理解的 var ReactClass = { /* * @param {object} spec Class specification (which must define `render`). * @return {function} Component constructor function. * @public */ createClass: function(spec) {
React.js
源代码,这就是我目前试图理解的
var ReactClass = {
/*
* @param {object} spec Class specification (which must define `render`).
* @return {function} Component constructor function.
* @public
*/
createClass: function(spec) {
var Constructor = function(props, context) {
// ...
this.props = props;
this.context = context;
this.state = null;
// ReactClasses doesn't have constructors. Instead, they use the
// getInitialState and componentWillMount methods for initialization.
var initialState = this.getInitialState ? this.getInitialState() : null;
// ...
this.state = initialState;
};
Constructor.prototype = new ReactClassComponent();
Constructor.prototype.constructor = Constructor;
// ...
mixSpecIntoComponent(Constructor, spec);
// ...
return Constructor; // <--- Who calls this?
},
// ...
};
var类={
/*
*@param{object}spec类规范(必须定义'render`)。
*@return{function}组件构造函数。
*@公众
*/
createClass:函数(规范){
var构造函数=函数(道具、上下文){
// ...
this.props=props;
this.context=上下文;
this.state=null;
//ReactClass没有构造函数,而是使用
//getInitialState和componentWillMount方法用于初始化。
var initialState=this.getInitialState?this.getInitialState():null;
// ...
this.state=初始状态;
};
Constructor.prototype=new ReactClassComponent();
Constructor.prototype.Constructor=构造函数;
// ...
mixSpecIntoComponent(构造器、规格);
// ...
返回构造函数;//这并不简单,您是对的
简而言之,React.render
调用Constructor
有关实际的实例化块,请参见此处:
基本路径如下:
- 创建一个
ReactClass
,其返回值为Constructor
- 当您创建工厂时(直接使用
React.createFactory
或通过JSX抽象地创建工厂),您的类将被绑定到React.createElement
作为type
,并返回绑定的函数(请参阅)
- 当您使用(或不使用)道具调用类时,实际上会调用此绑定函数
- 当将其作为
节点
传递到渲染
时,渲染
方法(上面链接)将元素
变量设置到节点
参数,然后直接实例化它或将其传递到其他lib进行实例化
好吧,只要你实例化一个新的MyReactClass,它就是called@Bergi在var inst=新组件(publicProps、publicContext)之外
我只是在源代码中的任何地方都找不到它…你在找哪一个源代码?我希望这些用户自定义的类是从应用程序代码中调用的,而不是从库代码中调用的。源文件。你从不调用你的ReactClass
实例,你只需将它们附加到React
树中。这就是我搜索的原因…你怎么样如果不使用React.createClass
定义组件,您就可以使用var inst=new Component(publicProps,publicContext);
,就是这样。这就是它被调用的地方。您希望看到什么?我希望我能不止一次地投票支持它!答案很好。