我试图弄清楚作为组件传入的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
});
}
用高度和宽度更新状态后,我尝试在getOrientation()
内部调用componentDidUpdate()
:
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.
有什么想法吗?
答案 0 :(得分:3)