我想渲染一个<section>
使用 React 的元素,但前提是满足某些条件。
问题是我无法嵌套 <section>
在<div>
内所以除了<body>
之外的最高级别的DOM元素是 <section>
我的 HTML:
<section id="my_element"></section>
我的 react :
ReactDOM.render(
<div className="container">
...
</div>,
document.getElementById('my_element')
);
我知道使用 Angular 指令,您可以使用 replace: true
将原始 DOM 元素替换为渲染的元素。
app.directive('myElement', function(){
return {
replace: true,
...
}
};
});
React 中有类似的东西吗?
最佳答案
目前尚未实现: https://github.com/facebook/react/issues/1311
关注本期查看进展: https://github.com/facebook/react/issues/1711
有解决方法的 StackOverflow 问题:
React.render replace container instead of inserting into
解决方法包括创建一个临时 div,然后替换节点。
关于javascript - 是否可以用 React 替换 DOM 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33535973/