React,使用gelElementById访问DOM,然后尝试用React组件替换节点

时间:2019-02-20 13:21:30

标签: javascript reactjs dom virtual-dom

如果我想用ReactJS节点替换DOM节点,怎么办?

const myComp = <span>hey</span>
const elem = document.getElementById('video1')
elem.parentNode.replaceChild(myComp, elem)

该怎么做?这不起作用

var temp = document.createElement('div')
ReactDOM.render(myComp, temp)
console.log(temp) // a node with the nested div element
console.log(temp.firstChild) // null (??)
console.log(temp.firstElementChild) // null (??)
elem.parentNode.replaceChild(temp.firstElementChild, elem) // error

1 个答案:

答案 0 :(得分:0)

const elem = document.getElementById('video1') 

...    

ReactDOM.render(myComp, elem)