最近一直寫React,慢慢就對里面的一些實現(xiàn)很好奇。最好奇的就是自定義標(biāo)簽的實現(xiàn)和this.setState的實現(xiàn)。這里不分析JSX是如何解析的,所有組件都用ES5方式編寫。
組件渲染
渲染時候,我們會調(diào)用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"));