<small id='Da42g'></small><noframes id='Da42g'>

    <i id='Da42g'><tr id='Da42g'><dt id='Da42g'><q id='Da42g'><span id='Da42g'><b id='Da42g'><form id='Da42g'><ins id='Da42g'></ins><ul id='Da42g'></ul><sub id='Da42g'></sub></form><legend id='Da42g'></legend><bdo id='Da42g'><pre id='Da42g'><center id='Da42g'></center></pre></bdo></b><th id='Da42g'></th></span></q></dt></tr></i><div id='Da42g'><tfoot id='Da42g'></tfoot><dl id='Da42g'><fieldset id='Da42g'></fieldset></dl></div>

  • <legend id='Da42g'><style id='Da42g'><dir id='Da42g'><q id='Da42g'></q></dir></style></legend>
      <bdo id='Da42g'></bdo><ul id='Da42g'></ul>

    1. <tfoot id='Da42g'></tfoot>

        ReactJS - 如何在同一页面中创建两个或多个组件

        时间:2023-09-29

        <small id='xPvHX'></small><noframes id='xPvHX'>

          <tbody id='xPvHX'></tbody>
          • <bdo id='xPvHX'></bdo><ul id='xPvHX'></ul>

          • <tfoot id='xPvHX'></tfoot>
          • <i id='xPvHX'><tr id='xPvHX'><dt id='xPvHX'><q id='xPvHX'><span id='xPvHX'><b id='xPvHX'><form id='xPvHX'><ins id='xPvHX'></ins><ul id='xPvHX'></ul><sub id='xPvHX'></sub></form><legend id='xPvHX'></legend><bdo id='xPvHX'><pre id='xPvHX'><center id='xPvHX'></center></pre></bdo></b><th id='xPvHX'></th></span></q></dt></tr></i><div id='xPvHX'><tfoot id='xPvHX'></tfoot><dl id='xPvHX'><fieldset id='xPvHX'></fieldset></dl></div>

            <legend id='xPvHX'><style id='xPvHX'><dir id='xPvHX'><q id='xPvHX'></q></dir></style></legend>

                1. 本文介绍了ReactJS - 如何在同一页面中创建两个或多个组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 ReactJS 的新用户,我遇到了一点问题.我正在创建一个通用地图组件,它运行良好.但是,只能逐页渲染一个组件.我想改用 getElementById,我可以使用 getElementsByClassName.

                  I'm new user of ReactJS and I bumped in a point. I'm creating a generic map component, it works perfectly. However, only can render one component by page. I would like to instead use getElementById, I could use getElementsByClassName.

                  有可能吗?我想保留我的系统通用,我在同一页面中使用许多组件.

                  Is it possible? I would like to leave my system generic, where I use many components in same page.

                  在示例中,我使用渲染 1 个地图,但如果我想使用 2 个或更多地图?

                  In the example, I use to render 1 map, but if I want to use with 2 or more maps?

                  <div class="MyMap" comp-url="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3538.7513696363594"></div>
                  <div class="MyMap" comp-url="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d322231313138.7513696363594"></div>
                  

                  我想使用:

                  ReactDOM.render( React.createElement(MyMap, null) , document.getElementsByClassName('MyMap'));
                  

                  带有 1 个地图示例的代码:

                  Code with 1 map example:

                  /******/ (function(modules) { // webpackBootstrap
                  /******/ 	// The module cache
                  /******/ 	var installedModules = {};
                  
                  /******/ 	// The require function
                  /******/ 	function __webpack_require__(moduleId) {
                  
                  /******/ 		// Check if module is in cache
                  /******/ 		if(installedModules[moduleId])
                  /******/ 			return installedModules[moduleId].exports;
                  
                  /******/ 		// Create a new module (and put it into the cache)
                  /******/ 		var module = installedModules[moduleId] = {
                  /******/ 			exports: {},
                  /******/ 			id: moduleId,
                  /******/ 			loaded: false
                  /******/ 		};
                  
                  /******/ 		// Execute the module function
                  /******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
                  
                  /******/ 		// Flag the module as loaded
                  /******/ 		module.loaded = true;
                  
                  /******/ 		// Return the exports of the module
                  /******/ 		return module.exports;
                  /******/ 	}
                  
                  
                  /******/ 	// expose the modules object (__webpack_modules__)
                  /******/ 	__webpack_require__.m = modules;
                  
                  /******/ 	// expose the module cache
                  /******/ 	__webpack_require__.c = installedModules;
                  
                  /******/ 	// __webpack_public_path__
                  /******/ 	__webpack_require__.p = "";
                  
                  /******/ 	// Load entry module and return exports
                  /******/ 	return __webpack_require__(0);
                  /******/ })
                  /************************************************************************/
                  /******/ ([
                  /* 0 */
                  /***/ function(module, exports) {
                  
                  	var MyMap = React.createClass({displayName: "MyMap",
                  	  getDefaultProps: function() {
                  	    return {
                  	      widthMap: 600,
                  	      heightMap: 450
                  	    }
                  	  },
                  	  getInitialState: function() {
                  	    return {
                  	      url: ''
                  	    }
                  	  },
                  	  componentWillMount: function() {
                  	    if ($('#MyMap').attr('comp-url') !== undefined) {
                  	      this.state.url = $('#MyMap').attr('comp-url');
                  	    }
                  	    this.setState({});
                  	  },
                  	  render: function() {
                  	    return (React.createElement("iframe", {src: this.state.url, width: this.props.widthMap, height: this.props.heightMap, frameborder: "0", allowfullscreen: true}));
                  	  }
                  	});
                  
                  	ReactDOM.render( React.createElement(MyMap, null) , document.getElementById('MyMap'));
                  
                  /***/ }
                  /******/ ]);

                  <html>
                  
                  <head>
                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
                    <script src="https://facebook.github.io/react/js/react.js"></script>
                    <script src="https://facebook.github.io/react/js/react-dom.js"></script>
                  </head>
                  
                  <body>
                    <div id="a"></div>
                    <div id="MyMap" comp-url="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3538.7513696363594!2d-48.51497268432733!3d-27.508106824924692!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x952746f0c86515b1%3A0xe8ccb33700020efe!2sCorporate+Park!5e0!3m2!1spt-BR!2sbr!4v1448470435091"></div>
                  </body>
                  
                  </html>

                  推荐答案

                  你可以只从 getElementsByClassName 迭代结果:

                  You can just iterate the result from getElementsByClassName:

                  function renderEls(elements) {
                      for (var i = 0; i < elements.length; i++) {
                          if( $(elements[i]).attr('comp-url') !== undefined ) {
                              var url = $(elements[i]).attr('comp-url');
                          }
                          ReactDOM.render(<MyMap url={url} />, elements[i]);
                      }
                  }
                  
                  renderEls(document.getElementsByClassName("MyMap"));
                  

                  这篇关于ReactJS - 如何在同一页面中创建两个或多个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将js设置状态从父组件反应到子组件 下一篇:反应孩子与父母的沟通问题

                  相关文章

                  <i id='29GWN'><tr id='29GWN'><dt id='29GWN'><q id='29GWN'><span id='29GWN'><b id='29GWN'><form id='29GWN'><ins id='29GWN'></ins><ul id='29GWN'></ul><sub id='29GWN'></sub></form><legend id='29GWN'></legend><bdo id='29GWN'><pre id='29GWN'><center id='29GWN'></center></pre></bdo></b><th id='29GWN'></th></span></q></dt></tr></i><div id='29GWN'><tfoot id='29GWN'></tfoot><dl id='29GWN'><fieldset id='29GWN'></fieldset></dl></div>

                    <legend id='29GWN'><style id='29GWN'><dir id='29GWN'><q id='29GWN'></q></dir></style></legend>

                    <small id='29GWN'></small><noframes id='29GWN'>

                  1. <tfoot id='29GWN'></tfoot>

                    • <bdo id='29GWN'></bdo><ul id='29GWN'></ul>