在渲染部件外部调用函数

时间:2018-04-29 23:24:14

标签: reactjs google-maps react-apollo

我是React和Apollo(GraphQL)的新手。当用户点击谷歌地图标记时,我需要显示一个图层。我的Apollo请求根据数据库中找到的任务正确显示标记,我面临的唯一问题是onClick返回TypeError: _this.handleMissionClick is not a function。我认为这是因为函数在render部分和main类之外,并且找不到链接这两个部分的方法。

这是完整的页面代码:

// @flow
import React from 'react'
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from "react-google-maps"
import gql from 'graphql-tag'
import { graphql } from 'react-apollo'
import { connect } from 'react-redux'
import Layer from 'grommet/components/Layer'
import MissionDetails from './../missions/details'

type TMission = {
  id : string,
  description: string,
  status: string,
  title: string,
  location: number[]
}

type TMissionProps = {
  data: {
    loading: boolean,
    searchMissions?: Array<TMission>
  }
}

function setIcon (status) {
  switch(status) {
    case 'accepted':
      return 'http://maps.google.com/mapfiles/ms/icons/green-dot.png';
    case 'created':
      return 'http://maps.google.com/mapfiles/ms/icons/red-dot.png';
    default:
      return null;
  }
}

const _MissionList = (props: TMissionProps) => {
  if (!props.data.searchMissions) return null
  return (
        props.data.searchMissions &&
        props.data.searchMissions.map( mission => (
            <Marker
                    position={{ lng: mission.location[0], lat: mission.location[1]}}
                    key={ mission.id }
                    title={ mission.title }
                    icon={setIcon(mission.status)}
                    onClick={() => this.handleMissionClick(mission.id)}
            >
            </Marker>
        ))
  )
}

const MissionList = connect(
  ({ session }) => ({ t: 1 })
)(graphql(gql`
    query mapMissions(
      $authToken: String!
    ) {
    searchMissions(
      input: {
        location: [2, 3]
      }
    ) {
      id
      title
      status
    }
  }
`, {
  options: {
    fetchPolicy: 'network-only'
  }
})(_MissionList))

const GoogleMapWrapper = withScriptjs(withGoogleMap((props) =>
    <GoogleMap
      defaultZoom={11}
      defaultCenter={{ lat: 48.8588377, lng: 2.2770201 }}
      center={props.center}
    >
      <MissionList/>
    </GoogleMap>))

export default  class DashboardPage extends React.Component {
  constructor () {
    super();
    this.state = {
      localised: {
        lat: 48.8588377,
        lng: 2.2770201,
        selectedMissionId: null,
        showMissionDetailsLayer: false
      }
    };
  }

  toggleMissionDetailsLayer = () => {
    this.setState({
      showMissionDetailsLayer: !this.state.showMissionDetailsLayer
    })
  }

  componentDidMount () {
    this.getLocation();
  }

  handleMissionClick = (missionId: string) => {
    this.setState({
      selectedMissionId: missionId
    })
    this.toggleMissionDetailsLayer()
  }

  getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition((position) => {
        this.setState({
          localised: {
            lat: position.coords.latitude,
            lng: position.coords.longitude
          }}
        )
      })
    }
  }

  render () {
    return (
      <div>
        <div style={{ height: '20vh', width: '100%' }}>

        </div>

          <GoogleMapWrapper isMarkerShown
                            googleMapURL="https://maps.googleapis.com/maps/api/js?key=$$$&?v=3.exp&libraries=geometry,drawing,places"
                            loadingElement={<div style={{ height: `100%` }} />}
                            containerElement={<div style={{ height: `400px` }} />}
                            mapElement={<div style={{ height: `80vh` }} />}
                            center={{ lat : this.state.localised.lat, lng : this.state.localised.lng }}

          />
        {
          this.state.showMissionDetailsLayer &&
          <Layer align='right' closer={true} overlayClose={true} onClose={this.toggleMissionDetailsLayer}>
            <MissionDetails mission={_(this.props.data.allMissions).find({id: this.state.selectedMissionId})} />
          </Layer>
        }
      </div>
    )
  }
}

1 个答案:

答案 0 :(得分:1)

由于您在DashboardPage中定义了 handleMissionClick 。哪个是MissionList的父级之一,你需要通过props传递回调处理程序。那样 MissionList 有一个处理onClick

的道具

在DashboardPage中,您应传递onClickHandler

<GoogleMapWrapper 
  isMarkerShown
  googleMapURL="https://maps.googleapis.com/maps/api/js?key=$$$&?v=3.exp&libraries=geometry,drawing,places"
  loadingElement={<div style={{ height: `100%` }} />}
  containerElement={<div style={{ height: `400px` }} />}
  mapElement={<div style={{ height: `80vh` }} />}
  center={{ lat : this.state.localised.lat, lng : this.state.localised.lng }}
  handleMissionClick={this.handleMissionClick} // Add callback prop here
/>

GoogleMapWrapper 中将handleMissionClick道具传递给 MissionList

const GoogleMapWrapper = withScriptjs(withGoogleMap((props) =>
<GoogleMap
  defaultZoom={11}
  defaultCenter={{ lat: 48.8588377, lng: 2.2770201 }}
  center={props.center}
>
  <MissionList handleMissionClick={props.handleMissionClick} />
</GoogleMap>))

在你的MissionProps中,你现在有onClick作为道具

type TMissionProps = {
  data: {
    loading: boolean,
    searchMissions?: Array<TMission>
  },
  onClick: string => void
} 

更新您的标记以使用回调道具

 <Marker
   position={{ lng: mission.location[0], lat: mission.location[1]}}
   key={ mission.id }
   title={ mission.title }
   icon={setIcon(mission.status)}
   onClick={() => props.handleMissionClick(mission.id)} // Notice this is now props.handleMissionClick instead of this.handleMissionClick
 >
 </Marker>