【发布时间】:2015-09-17 03:27:25
【问题描述】:
我有一个 DOM 元素 container,我想让 React 渲染到其中。
<div id="container">
<input id="ae06f4ec-5ce9-11e5-9f3f-0021cc62b713" class="child"/>
</div>
container 元素已经有一些具有我需要保留的属性的子元素:我需要保留子输入的 id。
当我调用React.render(myReactElement, document.getElementById('container')) 时,我是否可以在渲染覆盖内容之前从我的组件中获取container DOM 元素,这样我就可以获取 id 并将其存储为状态?
我尝试在componentWillMount 中的组件上调用getDOMNode,但我得到了
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 ``.
我试图避免将输入的 id 作为道具传递。 (这是一个简化的示例。我正在寻找一种可以扩展到容器元素的多个子元素的解决方案。)
【问题讨论】:
标签: javascript reactjs