[英]Is it possible to replace DOM elements with React?
I would like to render a <section>
element using React but only if certain conditions are met. 我想使用React渲染
<section>
元素,但仅限于满足某些条件。
The problem is I can't nest the <section>
within a <div>
so the highest level DOM element besides <body>
is <section>
问题是我无法将
<section>
嵌套在<div>
所以<body>
之外的最高级DOM元素是<section>
My HTML: 我的HTML:
<section id="my_element"></section>
My React: 我的反应:
ReactDOM.render(
<div className="container">
...
</div>,
document.getElementById('my_element')
);
I know with Angular directives you can replace the original DOM element with your rendered element by using replace: true
我知道使用Angular指令,您可以使用
replace: true
替换原始DOM元素和渲染元素
app.directive('myElement', function(){
return {
replace: true,
...
}
};
});
Is something similar available in React? 在React中有类似的东西吗?
Not currently implemented yet: https://github.com/facebook/react/issues/1311 目前尚未实施: https : //github.com/facebook/react/issues/1311
Follow this issue to see development: https://github.com/facebook/react/issues/1711 请按照此问题查看开发: https : //github.com/facebook/react/issues/1711
StackOverflow question that has a workaround: StackOverflow问题有一个解决方法:
React.render replace container instead of inserting into React.render替换容器而不是插入
Workaround involves creating a temporary div, and then replacing the node. 解决方法涉及创建临时div,然后替换节点。
You can use some case to da that. 你可以使用一些案例来达到这个目的。 I prefer do this:
我更喜欢这样做:
render(){
return [<div>test</div>, <div>test2</div>]
}
this is answer on your question: https://github.com/facebook/react/issues/2127 这是你的问题的答案: https : //github.com/facebook/react/issues/2127
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.