20

リーフレット マップ コンポーネントを作成しています。これは plunker の例です

http://plnkr.co/edit/LkghwOcby49XESdGb782?p=preview

ここにコードがあります

リーフレット.jsx

/** @jsx React.DOM */
/*jshint indent: 2, node: true, nomen: true, browser: true*/
/*global React */

'use strict';
module.exports = React.createClass({
  getInitialState: function () {
    return {
      map: {}
    };
  },
  componentDidMount: function() {
    var map = L.map('map').setView([51.505, -0.09], 13);
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    this.setState({map: map});
  },
  modifyMap: function (method, options) {
    this.setState({
      map : this.state.map[method](options.latLong, options.zoom, options.zoom_options)
    });
  },
  render: function () {
    return (
      /* jshint ignore:start */
      <div id="map"></div>
      /* jshint ignore:end */
    );
  }
});

map.html

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Leafy</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
    <!-- build:css(.tmp) styles/main.css -->
    <style>
      #map {
        height: 200px;
      }
    </style>
    <link rel="stylesheet" href="//cdn.leafletjs.com/leaflet-0.7.3/leaflet.css">
    <!-- endbuild -->
  </head>
  <body>
    <!--[if lt IE 9]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <div class="container">
      <div id="leaflet"></div>
    </div>

    <!-- build:js scripts/vendor.js -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/react/0.11.0/react-with-addons.min.js"></script>
    <script src="//cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
    <!-- endbuild -->

    <!-- build:js scripts/main.js -->
    <script src="leaflet.jsx"></script>
    <!-- endbuild -->
  </body>
</html>

I would like to try and call the modifyMap method directly from the browser console or from any script on the page. I have tried to use statics but I am unable to find the Object which contains the modifyMap method. Is there an easy way to find this method or is there a better way of doing this with Reactjs? I also know that this is possible if the React class was created on this page instead of using module.exports in a separate file, I am hoping there is another way! Please help!

4

2 に答える 2

8

コンポーネント メソッドを外部から呼び出します。

var app= React.renderComponent( <app/>, document.getElementById('app') );
app.modifyMap(arg1, arg2) //or anything else

または別のコンポーネントから:

componentDidMount: function(){
    this.refs.myApp.modifyMap(arg1, arg2) // Etc.
},
render: function(){
    return <div>
        <App ref='myApp'/>
    </div>
}

しかし、あなたの 'modifyMap' メソッドは反作用で考えています! componentDidMount を使用して、this.props のみに従ってマップに影響を与え、マップの親コンポーネントでそれらを変更します。Map インスタンスは次のようになります

<Map center={[51.505, -0.09]} zoom={13} [...] /> 
于 2014-08-20T20:20:40.170 に答える