How to get a React component's innerHTML

后端 未结 2 1467
灰色年华
灰色年华 2020-12-11 05:50

I have a react component and I want its innerHTML to pass as prop to an iframe.

render() {
        const page = 
        const iframeConte         


        
相关标签:
2条回答
  • 2020-12-11 06:17

    You can use refs or ReactDOM.findDOMNode to get innerHTML but the component should be mounted.

    class App extends React.Component{
    
      componentDidMount(){
        console.log(this.ref.innerHTML)
        console.log(ReactDOM.findDOMNode(this).innerHTML)
      }
      render(){
        return (
          <div ref={r=>this.ref = r}>app</div>
        )
      }
    }
    
    ReactDOM.render(
      <App />,document.getElementById("app")
    )
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <div id="app"></div>

    0 讨论(0)
  • 2020-12-11 06:42

    Maybe a bit late for a reply, but you may try reaching the react children elements through ref:

    const r = React.useRef(null);
    console.log(r.current.children)
    
    0 讨论(0)
提交回复
热议问题