我有一个名为 Parent 的组件,其中还有一个名为 Child 的组件:
<Parent>
<Child/>
</Parent>
因此生命周期如下:
我可以在第2步之后到第3步之前做其他的Parent初始化吗?
更新:
class ThirdPartyLib {
init(elementId) {
console.log(`initializing element: ${elementId}`);
// element with #id: elementId should exist!
// document.getElementById(elementId).style.color = "red";
}
}
class Parent extends React.Component {
constructor(props) {
super(props);
console.log("Parent's constructor");
}
render() {
console.log("rendering Parent");
new ThirdPartyLib().init("parent");
return (
<div id="parent">Parent: {this.props.name}
<Child name="Sara"/>
</div>
);
}
componentDidMount() {
console.log("Parent is mounted");
}
}
class Child extends React.Component {
constructor(props) {
super(props);
console.log(`Child ${this.props.name} constructor`);
}
render() {
console.log(`rendering Child: ${this.props.name}`);
return <div>Child: {this.props.name}</div>
}
componentDidMount() {
console.log(`Child ${this.props.name} is mounted`);
}
}
ReactDOM.render(<Parent name="Bob"/>, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>
在这里,我做了一些简化-我不仅在更改元素的颜色,还可以通过componentDidMount()
方法进行更改。相反,ThirdPartyLib
的详细信息规定了初始化顺序。我必须在父元素出现在DOM中之后,创建任何子元素之前立即对其进行初始化。
更具体地说,Parent
和Child
共享ThirdPartyLib
类的完全相同的实例。我不能将初始化逻辑放入Parent的render()
函数中,因为该元素还不在DOM中。同样,我无法按照Parent
的注释中的建议在Child
之前初始化componentDidMount()
,因为Child
的{{1}}在{{ 1}}。
答案 0 :(得分:2)
一种解决方法是将渲染子项延迟到挂载父项之后。步骤如下:
componentDidMount
执行第三方初始化并在“父母”状态下更改标志,以触发父母的重新呈现结果代码如下:
import React from "react";
import ReactDOM from "react-dom";
class ThirdPartyLib {
init(elementId) {
console.log(`initializing element: ${elementId}`);
this.element = document.getElementById(elementId);
this.element.style.color = "red";
}
}
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = { initialized: false };
console.log("Parent's constructor");
}
render() {
console.log("rendering Parent");
return (
<div id="parent">
Parent: {this.props.name}
{this.state.initialized && (
<Child name="Sara" thirdPartyLib={this.state.thirdPartyLib} />
)}
</div>
);
}
componentDidMount() {
console.log("Parent is mounted");
const thirdPartyLib = new ThirdPartyLib();
thirdPartyLib.init("parent");
this.setState({ initialized: true, thirdPartyLib });
}
}
class Child extends React.Component {
constructor(props) {
super(props);
console.log(`Child ${this.props.name} constructor`);
console.log(
`Child knows stuff from thirdPartyLib: ${
this.props.thirdPartyLib.element.id
}`
);
}
render() {
console.log(`rendering Child: ${this.props.name}`);
return (
<div>
Child: {this.props.name}
<br />
ThirdPartyLib element id:
{this.props.thirdPartyLib.element.id}
</div>
);
}
componentDidMount() {
console.log(`Child ${this.props.name} is mounted`);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Parent name="Bob" />, rootElement);