2017-08-03 118 views
0

我試圖呈現從父組件傳遞的特定位置的映射。我正在使用谷歌地圖反應,我不確定的兩件事:如何使用React渲染組件onClick?

如何調用函數onClick在我的渲染。以及如何在我的類中編寫呈現我想要的組件的函數。這是世界衛生大會TI迄今爲止:

import React, { Component } from 'react'; 
import yelp from 'yelp-fusion'; 
import xhr from 'xhr'; 
import GoogleMapContainer from './Map'; 

class BusinessCard extends Component { 
    constructor() { 
    super() 

    this.renderMap = this.renderMap.bind(this); 
    } 

    renderMap(){ 
    <GoogleMapContainer barLat={bar.coordinates.latitude} barLong={bar.coordinates.longitude} /> 
    } 

    render() { 
    const newCard = this.props.newCard 
    const bar = this.props.selectedBar 
    // console.log("this are the coordinates", bar["coordinates"]) 
    if(bar.coordinates){ 
     return (
     <div> 
      <p>{bar.coordinates.longitude}</p> 
      <p>{bar.name}</p> 
      <img src={bar.image_url} /> 
      <button> X </button> 
      <button onClick={newCard}> Yes </button> 

     </div> 
    ) 
    } else { 
     return(
     <div>Loading...</div> 
    ) 
    } 
    } 
} 

export default BusinessCard; 

目前,有與編譯一個問題,因爲bar渲染時是不確定的。任何建議/建議?

+0

你們是不是有選擇地呈現地圖如果this.props.selectedBar定義? – Matthew

+1

首先,你的'renderMap()'應該返回Jsx –

+1

而你並沒有在那裏使用 –

回答

2

首先,在React組件中,render()方法是虛擬DOM(由React保存在內存中)和具體DOM顯示給用戶的橋樑。我讀了更多關於React component's lifecycle - 理解這是理解反應。

此外,爲了在頁面中顯示您的GoogleMapContainer,您需要在React render()方法中調用您的方法renderMap(),將結果存儲在變量中並將其返回。

要完全可能調用onClick中的多個函數,請將函數傳遞給處理程序並調用您想要的函數。

檢查這個例子:

import React, { Component } from 'react'; 
import yelp from 'yelp-fusion'; 
import xhr from 'xhr'; 
import GoogleMapContainer from './Map'; 

class BusinessCard extends Component { 
    constructor() { 
    super() 

    // LOOK MORE WHAT 'this' means!! <- the key of javascript = execution context 
    this.renderMap = this.renderMap.bind(this); 
    this.handleClick = this.handleClick.bind(this); 
    } 

    renderMap(){ 
    // carefull!!! bar is undefined. Look more what 'this' means in javascript 
    const bar = this.props.selectedBar; 
    return (
     <GoogleMapContainer barLat={bar.coordinates.latitude} barLong={bar.coordinates.longitude} /> 
    ); 
    } 

    handleClick() { 
    const newCard = this.props.newCard; 

    // call the newCard function prop (if only is a function!!!) 
    newCard(); 

    // another method call 
    this.anotherMethod(); 
    } 

    anotherMethod() { 
    console.log('heyo!'); 
    } 

    render() { 
    const newCard = this.props.newCard 
    const bar = this.props.selectedBar 
    // console.log("this are the coordinates", bar["coordinates"]) 
    if(bar.coordinates){ 
     const renderMap = this.renderMap(); 
     return (
     <div> 
      <p>{bar.coordinates.longitude}</p> 
      <p>{bar.name}</p> 
      <img src={bar.image_url} /> 
      <button> X </button> 
      <button onClick={this.handleClick}> Yes </button> 
      { renderMap } 
     </div> 
    ) 
    } else { 
     return(
     <div>Loading...</div> 
    ) 
    } 
    } 
} 
+0

這真的很有幫助,非常感謝。 – user7496931