In want add react in my existing web app, I have an index.js file where there is a button and a thanks.js file or there is a thank you message, writing in jsx, now if someone clicks on the botton I display the message.
If I try to import thanks component into index.js it's not work, I get this error Uncaught ReferenceError: require is not defined
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My app</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
<div id="like_container"></div>
</div>
</div>
</div>
<!-- Load React. -->
<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>
<!-- Babel -->
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<!-- Load our React component. -->
<script src="js/src/index.js" type="text/babel"></script>
</body>
</html>
index.js
'use strict';
import Thanks from './thanks';
class Like extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
render() {
if (this.state.liked) {
return <Thanks />;
}
return (
<button onClick={() => this.setState({ liked: true }) }>
Like
</button>
);
}
}
let domContainer = document.querySelector('#like_container');
ReactDOM.render(<Like />, domContainer);
thanks.js
'use strict';
export default class Thanks extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<p>
Thanks you for liking!
</p>
);
}
}
Please help!