【问题标题】:Getting API callback to pass outside of function in ReactJS获取 API 回调以在 ReactJS 中的函数之外传递
【发布时间】:2016-07-08 04:52:06
【问题描述】:

我无法让数据通过第 72 行(我用断线标记)。到目前为止,一切都很好(来自数据库的数据正在传递给“google-geocoder”,并且“geoPlace”对象正在通过“数据”参数中的回调但是当我尝试访问“地理编码器”功能之外的“数据”时“数据”变得未定义。 我尝试过 .bind、setState、window. 等。但都没有运气。 我觉得这是一个范围问题,但不确定。 任何帮助将非常感激。谢谢你。

// Feed
//     Tools
//     Listing
//     Location
//         FeedMap
//           MapLoader

import React from 'react';
import geocoder from 'google-geocoder';
import FeedMap from './feedMap.js';

var Location = React.createClass({
  getInitialState: function(){
    return {
      petTrip: []
    }
   },

  getAllpetTripFromServer: function(){
    var self = this;
    $.ajax({
      method: 'GET',
      url: '/travel'
    }).done(function(data){
      console.log(data, "I am the data from line 17");
       self.setState({ petTrip: data })
     })
   },

  componentDidMount: function(){
     this.getAllpetTripFromServer();
   },

 //()
   render: function(){
     console.log(this.state.petTrip, 'I am the console log from line      28');
     return (
      <div>
           <AllpetTrip petTrip={this.state.petTrip}/>
      </div>
    )
  }

});


 var AllpetTrip = React.createClass({
  render: function(){
      // console.log(this.props.petTrip, "at map")
    var trips = this.props.petTrip.map(function(item){
      return <Geolocator start={item.startPoint}/>
    });

    return (
      <div>
        { trips }
      </div>
    )
   }
 });




var Geolocator = React.createClass({
  render: function(){
     var geo = geocoder ({
     key: 'AIzaSyC9Zst0uBpxGJ2P4LLv3IMATpN9Ppl4ImI'
      });
       geo.find(this.props.start, function(err, data){
       console.log(data, "this is the GeoPlace object")
        });

      return(
        <div>
        <FeedMap  geolocations = { data }/>
        </div>
    )
  }

});


 module.exports = Location;

【问题讨论】:

    标签: javascript google-maps reactjs google-geocoder


    【解决方案1】:

    这是因为您在收到这些之前尝试访问组件的属性。

    var AllpetTrip = React.createClass({
      componentWillReceiveProps(props){
        this.setState({
         petTrip: props.petTrip
        })
      },
      getInitialState(){
       return({
        petTrip: []
       })
      },
      render: function(){
          // console.log(this.props.petTrip, "at map")
        var trips = this.state.petTrip.map(function(item){
          return <Geolocator start={item.startPoint}/>
        });
    
        return (
          <div>
            { trips }
          </div>
        )
       }
     });
    

    或在您的代码上使用条件:

    var AllpetTrip = React.createClass({
      render: function(){
          // console.log(this.props.petTrip, "at map")
        var trips = this.props.petTrip.length ? this.props.petTrip.map(function(item){
          return <Geolocator start={item.startPoint}/>
        }) : null
    
        return (
          <div>
            { trips }
          </div>
        )
       }
     });
    

    我希望这会有所帮助。

    【讨论】:

    • 感谢您抽出宝贵时间查看此内容。那不是我遇到问题的地方,AllpetTrip 可以很好地传递它的数据。在“地理定位器”组件中,我无法将数据传递给我的 React Map。地理编码器函数工作并在回调中为我提供“数据”,但它不会返回/渲染另一个组件,即地图本身,或将回调中的“数据”传递给另一个组件。
    猜你喜欢
    • 2021-05-16
    • 2016-09-13
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多