简体   繁体   English

有没有办法在 React.render() 函数中渲染多个 React 组件?

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

For example could I do:例如我可以这样做:

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

React.render( 
  <PanelA />
  <PanelB />, 
  document.body  
);

where React would render: React 会在哪里渲染:

body
   PanelA
   PanelB

Currently I'm getting the error:目前我收到错误:

Adjacent JSX elements must be wrapped in an enclosing tag

while transpiling with browserify and babelify在使用 browserify 和 babelify 进行编译时

Since the React v16.0 release you can render an array of components without wrapping items in an extra element when you are inside a component:从 React v16.0 版本开始,当您在组件内部时,您可以渲染组件数组而无需将项目包装在额外的元素中:

render() {
  return [
    <li key="one">First item</li>,
    <li key="two">Second item</li>,
    <li key="three">Third item</li>,
    <li key="four">Fourth item</li>,
  ];
}

Remember only to set the keys.记住只设置键。

UPDATE更新

Now from the 16.2 version you can use the Fragments现在从 16.2 版本开始,您可以使用 Fragments

  render() {
    return (
      <React.Fragment>
        <td>Hello</td>
        <td>World</td>
      </React.Fragment>
    );
  }

Short syntax短句法

  render() {
    return (
      <>
        <td>Hello</td>
        <td>World</td>
      </>
    );
  }

In the ReactDOM.render you still can't render multiple items because react needs a root.ReactDOM.render你仍然不能渲染多个项目,因为 react 需要一个根。 So you can render a single component inside the ReactDOM.render and render an array of items in the internal component.因此,您可以在ReactDOM.render中渲染单个组件,并在内部组件中渲染一组项目。

React >= 16.2反应 >= 16.2

Since version 16.2 < React.Fragment /> (or <></> for short) was introduced, so you can use conditions.由于16.2版本引入了< React.Fragment /> (或简称<></> ),所以可以使用条件。

This is the preferable way.这是优选的方式。

return (
    <React.Fragment>
        <h1>Page title</h1>
        <ContentComponent />
        {this.props.showFooter && (
            <footer>(c) stackoverflow</footer>
        )}
    </React.Fragment>
)

React 16反应 16

Since React 16, you can return from the render() method a list of components.从 React 16 开始,您可以从render()方法返回组件列表。 The trade of is you cant easily condition the render and need to add key attribute to each component in the list.缺点是您无法轻松调整渲染,需要为列表中的每个组件添加key属性。

return [
    <h1 key="page-title">Page</h1>,
    <ContentComponent key="content" />,
    <footer>(c)stackoverflow</footer>
]

React < 16反应 < 16

In older versions of React, you can't render multiple components without wrapping them in an enclosing element (tag, component).在旧版本的 React 中,如果不将多个组件包装在一个封闭元素(标签、组件)中,就无法渲染多个组件。 eg:例如:

return (
  <article>
    <h1>title</h1>
    <meta>some meta</meta>
  </article>
)

If you want to use them realy as just one element, you have to create a module from them.如果您想真正将它们用作一个元素,则必须从它们创建一个模块。

Just wrap your multiple components into single tag.只需将多个组件包装到单个标签中即可。 For example:例如:

React.render(
  <div>
    <PanelA />
    <PanelB />
  </div>, 
  document.body  
);

Prior to React 16, multiple top-level elements in the same render() would require you to wrap everything in a parent element (typically a <div> ):在 React 16 之前,同一个render()中的多个顶级元素将要求您将所有内容包装在父元素中(通常是<div> ):

render () {
  return (
    <div>
      <Thing1 />
      <Thing2 />
    </div>
  )
}

React 16 introduced the ability to render an array of top-level elements (with a warning that they all must have unique keys): React 16 引入了渲染顶级元素数组的能力(警告它们都必须具有唯一键):

render () {
  return ([
    <Thing1 key='thing-1' />,
    <Thing2 key='thing-2' />
  ])
}

React 16.2 introduced the <Fragment> element, which functions exactly like the <div> in the first example but doesn't leave a pointless parent <div> hanging around the DOM: React 16.2 引入了<Fragment>元素,它的功能与第一个示例中的<div>完全相同,但不会在 DOM 周围留下无意义的父<div>

import React, { Fragment } from 'react'

...

render () {
  return (
    <Fragment>
      <Thing1 />
      <Thing2 />
    </Fragment>
  )
}

There's a shorthand syntax for it, but it isn't supported by most tooling (eg, syntax highlighters) yet:它有一个简写语法,但大多数工具(例如语法荧光笔)还不支持它:

import React from 'react'

...

render () {
  return (
    <>
      <Thing1 />
      <Thing2 />
    </>
  )
}

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 <div>您可以只使用一个 DOM 元素,例如<div>

  <div>
    <PanelA />
    <PanelB />
  </div>

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 (
      <div>
        <PanelA />
        <PanelB />
      </div>
    )
  }
});

And then in your main js file, you would do:然后在您的主 js 文件中,您将执行以下操作:

import React from 'react';
import PanelHolder from './panelHolder.jsx';

React.render( 
  <PanelHolder /> 
  document.body  
);
this.propsTextBox.value = this._context.parameters.textBoxProperty.raw || "";
var elements = new Array<React.SFCElement<any>>();
elements.push(React.createElement(
                TextField, 
                this.propsTextBox
            ));
elements.push(React.createElement(
    Label, 
    this.propsLabel
));

ReactDOM.render(
    elements,  
    this._container
);

You can do it like this too.你也可以这样做。

const list = [item1, item2]
const elements = (
    <div>
      {list.map(element => element)}
    </div>
  );
ReactDOM.render(elements, document.body);
React.render( 
  <PanelA>
      <PanelB />
  </PanelA> 
  document.body  
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM