【发布时间】:2018-03-04 16:09:29
【问题描述】:
我正在尝试对其进行设置,以便在滚动从数组映射的图像时,使用该图像的坐标更新状态,从而更新 Google 地图。
<CloudinaryContext cloudName="hcjmhcjf" fetchFormat="auto">
<div className="image-holder">
{displayImages.map((displayImage, i) => {
return (
<div
className="responsive"
key={i}
ref="img"
onScroll={this.handleScroll(this.state.images[i].location)}>
<div className="img">
<a
target="_blank"
href={`http://res.cloudinary.com/gdbdtb/image/upload/${displayImage}.jpg`}>
<Image publicId={displayImage} responsive style={{width: '100%'}}>
<Transformation crop="scale" width="auto" responsive_placeholder="blank" />
</Image>
</a>
</div>
</div>
);
})}
</div>
</CloudinaryContext>
通过阅读其他一些问题,我设置了以下生命周期方法:
componentDidMount() {
const imgDiv = ReactDOM.findDOMNode(this.refs.img)
imgDiv.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
const imgDiv = ReactDOM.findDOMNode(this.refs.img)
imgDiv.removeEventListener('scroll', this.handleScroll);
}
但是,ReactDOM.findDOMNode(this.refs.img) 总是返回 undefined
我可以打电话的方式是什么
handleScroll(location) {
this.setState({
center: location
})
}
并在滚动特定图像时更新地图?
【问题讨论】:
-
您能说明一下您的要求吗?我不明白以下内容:“当滚动从数组映射的图像时,状态将使用该图像的坐标进行更新”
-
@MatthewBarbara 所以我有一组与谷歌地图相邻的图像。在第一次渲染时,谷歌地图以第一张图像的坐标为中心。我想要的是当滚动图像时,谷歌地图将重新以滚动图像为中心
-
你能把你的问题放在 codepen 或 jsfiddle 上吗?会更容易理解问题
标签: javascript reactjs dom refs