Before starting: -Can't use Node, jsx, extensions or any other thing (rule in my work, nothing I can do about it) -I need an HTML file with references to other scripts -I have problems with babel...
What I need is an HTML file with references to other scripts (NOT EVERYTHING ON THE HTML file), and being able to write "normal" HTML in them...
So, I have this code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Add React in One Minute</title>
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
</head>
<body>
<h1>HTML with multiple scripts using React.js</h1>
<p>React.js without Node.js or any other thing</p>
<p>React.js is loaded as script tags.</p>
<div>
<div id="like_button_container"></div>
<div id="like_button_container2"></div>
</div>
<script src="like_button.js"></script>
<script src="like_button2.js"></script>
</body>
</html>
like_button.js :
'use strict';
const e = React.createElement;
class LikeButton extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
render() {
if (this.state.liked) {
return 'You liked this.';
}
return e(
'button',
{ onClick: () => this.setState({ liked: true }) },
'Like'
);
}
}
const domContainer = document.querySelector('#like_button_container');
ReactDOM.render(e(LikeButton), domContainer);
like_button2.js :
'use strict';
const f = React.createElement;
class LikeButton2 extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
render() {
if (this.state.liked) {
return 'You liked this.';
}
return f(
'h1',
{ onClick: () => this.setState({ liked: true }) },
'Like'
);
}
}
const domContainer2 = document.querySelector('#like_button_container2');
ReactDOM.render(f(LikeButton2), domContainer2);
And as you can see, I can't write "normal" HTML, I have to do it within quotation marks... which is really limiting and ugly:
return e(
'button',
{ onClick: () => this.setState({ liked: true }) },
'Like'
);
If I try to write "normal" HTML:
return e(
<button>WASD</button>
);
I get the error: Unexpected token '<':
https://i.stack.imgur.com/szNjA.png
NOW NOW NOW, I know that you will tell me to import/use babel, but if I do and put the "text/babel" attribute in the script tag, I get this error: 'Access to XMLHttpRequest at 'file:///C:/Users/edunec/Desktop/wasd/like_button.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.'
https://i.stack.imgur.com/gtc3R.png
Please someone help me, as I said, I need an HTML file with references to other scripts (not everything in the same script), and being able to write "normal" HTML... can't use Node or jsx or any other thing, it has to be done this way (rule at work).
If for some reason this is not possible (doubt it), I need to know how to write HTML within quotation marks and how to do more complicated things with it (like one element inside the other...), and If someone knows how to render both scripts to the same div within a parent wrapper, I'll be thankful too.
Thank you in advance.