十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
最近一直写React,慢慢就对里面的一些实现很好奇。最好奇的就是自定义标签的实现和this.setState的实现。这里不分析JSX是如何解析的,所有组件都用ES5方式编写。
组件渲染
渲染时候,我们会调用render方法。类似下面这样:
var SayHi = React.createClass({ getInitialState: function() { return {verb: 'say:'}; }, componentWillMount: function() { console.log('I will mount'); }, componentDidMount: function() { console.log('I have mounted'); }, render: function() { return React.createElement("div", null,this.state.verb, "Hello ", this.props.name); } }); React.render(React.createElement(SayHi, {name: "Cynthia"}), document.getElementById("container"));