简体   繁体   English

分别渲染每个React.js子组件

[英]Rendering separately each React.js child component

I am using React.js to render out most of my HTML, and this works fine. 我正在使用React.js来渲染我的大部分HTML,这很好用。 However on the part that uses a cms framework this expects each component to be inside some auto generated html that the cms creates which i am unable to generate in the react code. 但是在使用cms框架的部分,这要求每个组件都在cms创建的一些自动生成的html中,我无法在反应代码中生成。 Example below: 示例如下:

React generated code 反应生成的代码

<div class=“react-component-1”>
    <img src=“foo.png”/>
</div>
<div class=“react-component-2”>
    <p>Some text</p>
</div>

CMS CMS

<div class=“some-generated-value”>
    <div class=“react-component-1”>
        <img src=“foo.png”/>
    </div>
</div>

<div class=“some-generated-value”>
    <div class=“react-component-2”>
        <p>Some text</p>
    </div>
</div>

My React.js code has essentially a single component/container for the entire page that then has many child components. 我的React.js代码基本上包含整个页面的单个组件/容器,然后有许多子组件。 What I would like to do is to create the React.js component as normal but then somehow insert each child into its relevant div which i can work out via Ids. 我想做的是正常创建React.js组件,但然后以某种方式将每个子进入其相关的div,我可以通过ID解决。 Is it possible to somehow access each of these child components and render them separately in there respective auto generated divs? 是否有可能以某种方式访问​​这些子组件中的每一个并在相应的自动生成的div中单独呈现它们? I've tried looking at the object created by React.createElement on the top level component but I can't work out how to identify the child components/access there properties. 我已经尝试在顶级组件上查看由React.createElement创建的对象,但我无法弄清楚如何识别子组件/访问那些属性。

You can have a look at ReactDOM.render(). 你可以看一下ReactDOM.render()。 This allows you to inject your component separately in any container you like. 这允许您将组件单独注入任何您喜欢的容器中。 This depends on which React version you are using though. 这取决于您使用的React版本。 This is 14+ I believe. 我相信这是14岁以上。 This page explains the top level API https://facebook.github.io/react/blog/2015/10/01/react-render-and-top-level-api.html 此页面解释了顶级API https://facebook.github.io/react/blog/2015/10/01/react-render-and-top-level-api.html

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

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