
The iFrame has two important properties: src and srcdoc. The first one allows you to embed an external page, while the second one takes HTML as input. For this guide and approach, we will be using only srcdoc by pasting custom code into it.
<h1>Hello</h1><h3>world</h3>
<script>
tag. Try inputting the following code:<button onclick=handleClick()>Click me!</button>
<script>
function handleClick() {
alert("You clicked!")
}
</script>
<script>
's src
attribute to import JavaScript from external resources. We can use a Content Delivery Network (CDN) to import a hosted version of React and use it in our code. We will also use BabelJS to write modern JavaScript without compatibility issues. With that, our list of imports will look the following way:<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/7.21.4/babel.min.js"></script>
<script>
tag. We are going to use React Class Components that contain the component within render(){return <div></div>}
structure, but you can use function components as well. Find the code below:<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/7.21.4/babel.min.js"></script>
<script type="text/babel">
class Greeting extends React.Component {
render() {
return (<p>Hello World!</p>);
}
}
ReactDOM.render(
<Greeting />,
document.getElementById('root')
);
</script>
<div>
tag with id="root"
and render a component inside of it, written entirely in React.<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/7.21.4/babel.min.js"></script></script>
<script type="text/babel">
class Greeting extends React.Component {
state = {
total_clicks: 0,
};
add = () => {
this.setState({
total_clicks: Math.random(),
});
};
render() {
return (
<div>
<h2>Clicks: {this.state.total_clicks}</h2>
<button onClick={this.add}> Click here! </button>
</div>
);
}
}
ReactDOM.render(<Greeting />, document.getElementById("root"));
</script>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
11 | |
8 | |
6 | |
6 | |
6 | |
5 | |
5 | |
5 | |
5 |