import React, { Component } from 'react';
class Children extends Component {
constructor(props) {
super(props);
console.log('son constructor');
}
componentDidMount() {
console.log('son mounted')
}
render() {
console.log('son render')
return(
<div>son</div>
)
}
}
export { Children };
class Parent extends Component {
constructor(props){
super(props);
console.log('parent constructor')
}
componentDidMount() {
console.log('parent mounted')
}
render() {
console.log('parent render')
const { children } = this.props;
return(
<div>
<p>parent</p>
<div>{children}</div>
</div>
)
}
}
export {Parent};
打印结果:
思考:在componentDidMount执行之前先render一次,目的就是先去渲染子组件;
挂载dom节点是一个由内向外的过程。