2017-09-23 46 views
0

我試圖設置它,以便在從一個數組映射的映像滾動時,狀態會更新爲該映像的座標,從而更新Google Map。React映射數組中的OnScroll事件偵聽器

<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 
    }) 
} 

並且在規範中有地圖更新圖像是否滾動?

+0

你能澄清你的要求嗎?我不明白以下內容:「當從一個數組映射的圖像滾動時,狀態會更新爲該圖像的座標」 –

+0

@MatthewBarbara,所以我有一個圖像陣列毗鄰谷歌地圖。在第一次渲染時,谷歌地圖以第一張圖片的座標爲中心。我想要的是當圖像滾動時,谷歌地圖將重新居中滾動的圖像 –

+0

你可以把你的問題放在codepen或jsfiddle上嗎?這將更容易理解這個問題 –

回答

0

如果這能幫助別人,我使用做出反應,航點的插件解決了這個問題:

import React, { Component } from 'react' 
import { Header, ImageBoard, AccountNav, AlbumBoard } from '../modules' 
import {Router, Route, Redirect, Link, withRouter } from 'react-router-dom' 
import { APIManager } from '../utils' 
import ReactDOM from 'react-dom' 
import {Image, CloudinaryContext, Transformation} from 'cloudinary-react'; 
import { compose, withProps, lifecycle } from "recompose"; 
const Waypoint = require('react-waypoint'); 
import { 
withScriptjs, 
withGoogleMap, 
GoogleMap, 
Marker 
} from "react-google-maps"; 


export default class Album extends Component { 
constructor(){ 
    super() 
    this.toLatLng.bind(this), 
    this.handleScroll = this.handleScroll.bind(this), 
    this.state = { 
     images: [], 
     center: { lat: 25.03, lng: 121.6 } 
    } 
} 

toLatLng (coord) { 
    const latLng = {lat: coord[1], lng: coord[0]} 
    return latLng 
} 

componentWillMount(){ 
    APIManager.get(`/api/album/${this.props.match.params.id}`, null, (err, response) => { 
     if(err){ 
      let msg = err.message || err 
      console.log(msg) 
      return 
     } 

     let images = response.result[0].albums.images 

     this.setState({ 
      images: images, 
      center: this.toLatLng(images[0].location) 
     }) 
    }) 
} 


handleScroll(location){ 
    this.setState({ 
     center: this.toLatLng(location) 
    }) 
} 


render(){ 

    const Map = compose(
      withProps({ 
      googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyAsv9Hz2-CqDOgrb4FBSkfC-4aTiJL13cI", 
      loadingElement: <div style={{ height: `100%` }} />, 
      containerElement: <div style={{ height: `calc(100vh - 100px)`, position: 'fixed', left: '70vw' }} />, 
      mapElement: <div style={{ width: `30vw`, height: `100%`}} />, 
      center: this.state.center, 
      }), 
      withScriptjs, 
      withGoogleMap, 
     )(props => 
      <GoogleMap 
      defaultZoom={14} 
      defaultCenter={props.center} 
      > 
      {this.state.images.map((image, i) => { 
       return (
        <Marker key={i} 
         position={this.toLatLng(image.location)} 
        /> 
      ) 
      })} 
      </GoogleMap> 
     ); 


    const toPublicId = (image) => { 
     return image.slice(62, image.length) 
    } 


    return(
     <div> 
      <Header /> 
      <Map /> 
      <div className="albumImageBoard"> 
       <CloudinaryContext cloudName="djswgrool" fetchFormat="auto" > 
        <div className="image-holder" ref="imgDiv"> 

         {this.state.images.map((image, i) => { 
          return (
           <div className="responsive" key={i}> 
            <div className="img"> 
             <a target="_blank" href={`http://res.cloudinary.com/djswgrool/image/upload/${toPublicId(image.url)}.jpg`}> 
              <Image publicId={toPublicId(image.url)} responsive style={{width: '100%'}} > 
               <Transformation 
                crop="scale" 
                width="auto" 
                responsive_placeholder="blank" 
               /> 
              </Image> 
             </a> 
            </div> 
            <Waypoint 
            onEnter={() => this.handleScroll(image.location)}/> 
           </div> 

         ) 
         }) 
        } 
        </div> 
       </CloudinaryContext> 
      </div> 
     </div> 
    ) 
} 

}