【发布时间】:2019-04-24 14:10:17
【问题描述】:
我正在尝试找出作为道具传入的 React 组件中背景图像的方向。
我首先创建一个 Image 对象并将其 src 设置为新的 Image:
getImage() {
const src = this.props.url;
const image = new Image();
image.src = src;
this.setState({
height: image.height,
width: image.width
});
}
用高度和宽度更新状态后,我尝试在componentDidUpdate() 内部调用getOrientation():
getOrientation() {
const { height, width } = this.state;
if (height > width) {
this.setState({ orientation: "portrait" });
} else {
this.setState({ orientation: "landscape" });
}
}
然后我得到以下错误:
Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
有什么想法吗?
【问题讨论】:
标签: javascript reactjs image orientation setstate