【问题标题】:How can I call methods on Reactjs components in the browser console or from an outside script?如何在浏览器控制台或外部脚本中调用 Reactjs 组件的方法?
【发布时间】:2014-10-04 16:25:08
【问题描述】:

我正在创建一个传单地图组件,这是 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>

我想尝试直接从浏览器控制台或页面上的任何脚本调用 modifyMap 方法。我曾尝试使用静态,但我无法找到包含 modifyMap 方法的对象。有没有一种简单的方法可以找到这种方法,或者有没有更好的方法来使用 Reactjs?我也知道,如果在此页面上创建 React 类而不是在单独的文件中使用 module.exports,这是可能的,我希望有另一种方法!请帮忙!

【问题讨论】:

    标签: javascript reactjs react-jsx


    【解决方案1】:

    如果你想调用组件instance上的方法,你需要在组件生命周期的某个时刻让它可用。例如,如果页面上只有一张这样的地图,您可以这样做:

    componentDidMount: function() {
      // ...
      this.setState({map: map});
      window.map = this;
    }
    

    现在,您可以从全局 map 变量访问组件:

    标准免责声明:虽然这有时会很有用,尤其是在调试时,但如果你发现自己经常这样做,你应该重新审视你对 React 的使用——也就是说,你通常应该将属性传递给组件,组件然后将用于更新自身。

    【讨论】:

    • 谢谢,是的,我应该想到的
    • 这太可怕了。谈谈副作用
    • 天啊。你知道人们忽略了否认并在产品中使用它。
    • 这会有什么副作用?对于像我这样刚开始做出反应并试图在产品中做到这一点的人
    • 我认为是出于安全原因,因为任何人都可以调用并更新您地图的值
    【解决方案2】:

    在外部调用组件方法:

    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 实例应该类似于

    <Map center={[51.505, -0.09]} zoom={13} [...] /> 
    

    【讨论】:

    • 在 中不起作用 我正在为我的组件使用
    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 2021-02-26
    • 1970-01-01
    • 2012-04-22
    相关资源
    最近更新 更多