I'm trying to render a simple react component to a html page, but can't figure out how to do it.
// hello.js
import React from 'react';
import ReactDOM from 'react-dom';
export default class Hello extends React.Component {
render() {
return (
<h1>Hello {this.props.name}</h1>
)
}
}
// index.html
<html>
<body>
</body>
<head>
<!-- HTML code -->
<!-- RENDER HERE -->
</head>
</html>
webpack and babel:
// index.js
import Hello from './hello.js'
If i put this line at the end of Hello.js:
ReactDOM.render(<Hello/>, document.getElementById('hello'));
I can render the component with:
// index.html
<div id="hello"></div>
<script src="bundle.js"></script>
However, There are times where I don't want to render the component, or render it after a certain time interval. If I remove the div i get an error:
Target container is not a DOM element
I guess I want to do something like this (Which doesn't work)
<html>
<body>
</body>
<head>
<!-- HTML code -->
<!-- RENDER HERE -->
<div id="hello"></div>
<script src="bundle.js"></script>
<script>
setTimeout(function() {
ReactDOM.render(<Hello/>, document.getElementById('hello'));
},1000);
// or
if (true) {
ReactDOM.render(<Hello/>, document.getElementById('hello'));
}
</script>
</head>
</html>
What am I doing wrong here? And question nr 2. How do I pass props to the component?