【发布时间】:2020-09-22 08:23:59
【问题描述】:
我有一个模式显示我在道具中收到的数据。当我打开模式时,我应该会看到从我的道具中显示的选择数据。但是,模式在我第一次打开时为空,第二次填充。
如果我继续更改道具中的数据,模式在第一次新点击时保持不变,并在第二次新点击时刷新。
我试过用setTimeout 强制它,弄乱componentDidMount、componentDidUpdate 和其他生命周期方法的组合,但似乎没有任何效果。我确定这与我在componentDidMount 中使用prevData 参数有关。但即使反应开发工具显示this.state.pricesData 更新,当我尝试从状态渲染时,我每次都会得到空白。当我调用控制台日志作为 setState 的回调时,我在控制台日志中得到一个空数组括号(我可以展开它以显示所有正确的数组数据,但我猜这是在日志之后异步填充的)。
代码如下:
import React, { Component } from "react";
import "../../App.css";
let explanations = [];
export default class ExplanationModal extends Component {
constructor(props) {
super(props);
this.state = {
pricesData: [],
};
}
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.pricesData !== prevState.pricesData) {
return { pricesData: nextProps.pricesData };
} else {
return null;
}
}
// to allow for async rendering
getSnapshotBeforeUpdate(prevProps) {
if (prevProps.pricesData !== this.state.pricesData) {
return this.state.pricesData;
}
}
componentDidMount = () => {
this.setState({ pricesData: this.props.pricesData }, () =>
console.log(this.state.pricesData)
);
};
componentDidUpdate = (prevData) => {
this.renderExp(prevData.pricesData);
};
renderExp = (data) => {
explanations = [];
data.forEach((set) =>
explanations.push({ title: set.titel, explanation: set.explenation })
);
};
onClose = () => {
this.props.hideModal();
};
render() {
return (
<div className="modal">
<div>
{explanations.map((item) => (
<span>
<h4>{item.title}</h4>
<p>{item.explanation}</p>
</span>
))}
</div>
<button onClick={this.onClose} className="update">
Close
</button>
</div>
);
}
}
【问题讨论】:
标签: javascript reactjs state react-props react-lifecycle