How to render a component onClick with React?

user7496931 picture user7496931 · Aug 3, 2017 · Viewed 42.1k times · Source

I'm trying to render a map of a specific location that's being passed from a parent component. I'm using google-maps-react and i'm unsure of two things:

How to call to functions with onClick inside my render. And how to write the function in my class that renders the component I want. This is wha tI have so far:

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;

At the moment, there's a problem with that compiling since bar is undefined when rendering. Any suggestions/advice?

Answer

Dragos Rizescu picture Dragos Rizescu · Aug 3, 2017

First of all, in a React component, the render() method is your bridge between the virtual DOM (kept in memory by React) and the concrete DOM that is shown to the user. I'd read more about a React component's lifecycle - understanding this is understanding react.

Furthermore, in order to show your GoogleMapContainer on the page, you need to call your method renderMap() in the React render() method, store the result in a variable and return it.

For calling multiple functions in onClick that is totally possible, pass a function to the handler and call how many functions you want there.

Check this example:

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

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

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

  renderMap(){
    // careful!!! 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>
      )
    }
  }
}