I am adding react to my existing html page. by following the instructions here enter link description here. And then this works just fine, until I import a different component.
my html codes:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test2</title>
</head>
<body>
<h1>Hello World</h1>
<div id="like_button_container"></div>
<!-- Load React. -->
<!-- Note: when deploying, replace "development.js" with "production.min.js". -->
<script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin>.
</script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
crossorigin></script>
<!-- Load our React component. -->
<script src="like_button.js"></script>
</body>
</html>
my like_button.js codes:
'use strict';
import Another from "./another.js"; //problem here, if you comment this line, it works.
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);
and my another.js codes:
'use strict';
const e = React.createElement;
class Another extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
render() {
if (this.state.liked) {
return 'You liked this (another).';
}
return e(
'button',
{ onClick: () => this.setState({ liked: true }) },
'Like'
);
}
}
export default Another;
thank you for your answer.
edit: i added "type": "module" in package.json file. The latest situation is as follows;
{
"name": "test2",
"version": "1.0.0",
"description": "",
"main": "another.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module"
}
but it still didn't work, I get the same error in the console.
edit2: fix the problem. Just adding ("type": "module") to package.json was not enough, I also had to add (type="module") to the script tag in the html file.