Javascript 是否可以在覆盖内容之前将容器DOM元素传递给React.render?

Javascript 是否可以在覆盖内容之前将容器DOM元素传递给React.render?,javascript,reactjs,Javascript,Reactjs,我有一个DOM元素,container,我想在其中进行React渲染 <div id="container"> <input id="ae06f4ec-5ce9-11e5-9f3f-0021cc62b713" class="child"/> </div> 我试图避免将输入的id作为道具传递。(这是一个简化的示例。我正在寻找一个可扩展到容器元素的多个子元素的解决方案。)在从组件内部渲染之前,没有办法获取容器内容,因此我认为您无法避免使用道具。然而,这并不意

我有一个DOM元素,
container
,我想在其中进行React渲染

<div id="container">
  <input id="ae06f4ec-5ce9-11e5-9f3f-0021cc62b713" class="child"/>
</div>

我试图避免将输入的id作为道具传递。(这是一个简化的示例。我正在寻找一个可扩展到容器元素的多个子元素的解决方案。)

在从组件内部渲染之前,没有办法获取容器内容,因此我认为您无法避免使用道具。然而,这并不意味着你必须作为道具传递数据;您可以简单地捕获DOM节点并将它们作为道具传递,让组件完成工作。例如:

函数渲染(元素、容器){
//将容器的子级(作为DOM节点)传递给顶级组件
var cloned=React.cloneElement(元素,{originalChildren:container.children})
React.render(克隆,容器);
}
var Application=React.createClass({
getInitialState(){
//将节点列表转换为数组。
var originalChildren=Array.prototype.slice.call(this.props.originalChildren);
var id=originalChildren.map(节点=>{
返回node.getAttribute(“id”);
});
返回{
ids:ids
};
},
render(){
返回(
id是:{this.state.IDs.map(id=>{id}}
);
}
});
//现在,我们使用相同的参数调用'render'
//用于“React.render”;不需要做任何特别的事情。

render(:

谢谢。这似乎是一种合理的方法。
Invariant Violation: findComponentRoot(..., .0): Unable to find element. This probably means the DOM was unexpectedly mutated (e.g., by the browser), usually due to forgetting a <tbody> when using tables, nesting tags like <form>, <p>, or <a>, or using non-SVG elements in an <svg> parent. Try inspecting the child nodes of the element with React ID ``.