I have a react component and I want its innerHTML to pass as prop to an iframe.
render() {
const page = <PrintPage />
const iframeContent = page..something...innerHTML
return (
<iframe srcDoc={iframeContent}/>);
}
Is there any way for doing like this.
I want to render only within the iframe, so i can't use ref. I want the innerHTML of component which is not mounted
When the button element is clicked, the click event gets triggered, the value of the tagName property can be used to find out HTML element the source of the event and innerText property can be used to find out text written in that HTML element.
This rule applies when innerHTML prop for a React DOM element is used. innerHTML prop is risky because it is easy to expose your users to a cross-site scripting (XSS) attack.
In React we can access the DOM element using Refs. Refs provide a way to access DOM nodes or React elements created in the render method. Creating Refs: Refs are created using React. createRef() and attached to React elements via the ref attribute.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With