D
D
DarthJS2017-10-03 21:10:59
JavaScript
DarthJS, 2017-10-03 21:10:59

How to get innerHTML React element by ref?

There is an element like

<div ref={el = this.TestElement = el } style={{...}}><h1>xxx</h1></div>

how do I convert this.TestElement so I can use innerHTML as a string?
That is, I need to get the DOM element in the form of a string

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danakt Frost, 2017-10-03
@DarthJS

const testElementHtml = this.TestElement.parentNode.innerHTML

But in this case, the HTML of all child elements of the parent node will be obtained
. If there are other child elements in addition to this div, then this:
const testElementClone = this.TestElement.cloneNode(true)
const wrap = document.createElement('div')
wrap.appendChild(testElementClone)
const testElementHtml = wrap.innerHTML

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question