Created
December 12, 2020 13:13
-
-
Save olygood/bd461dd3e9e90186ad70cf09a03f402e to your computer and use it in GitHub Desktop.
react: ajouter react a un site web existant.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//html | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Add React in One Minute</title> | |
</head> | |
<body> | |
<h2>Add React in One Minute</h2> | |
<p>This page demonstrates using React with no build tooling.</p> | |
<p>React is loaded as a script tag.</p> | |
<!-- We will put our React component inside this div. --> | |
<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> | |
//js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment