Is there a way to render multiple React components in the React.render() function?

后端 未结 8 1249
无人共我
无人共我 2020-12-08 06:33

For example could I do:

import React from \'react\';
import PanelA from \'./panelA.jsx\';
import PanelB from \'./panelB.jsx\';

React.render( 
  

        
8条回答
  •  旧巷少年郎
    2020-12-08 06:48

    If you wish to render multiple components out you need to nest them within one another in order to maintain the Tree-Like structure. This is explained on their docs page for Multiple Components

    Ultimately as long as there is one Node at the top level it can work.

    You could use just one DOM element such as a

      

    However as you create more complex apps and have more interlinking components you may find it best to wrap child components in a parent like so

    import React from 'react';
    import PanelA from './panelA.jsx';
    import PanelB from './panelB.jsx';
    
    var PanelHolder = React.createClass({
      render: function() {
        return (
          
    ) } });

    And then in your main js file, you would do:

    import React from 'react';
    import PanelHolder from './panelHolder.jsx';
    
    React.render( 
       
      document.body  
    );
    

提交回复
热议问题