Skip to content

Instantly share code, notes, and snippets.

@gaearon
Forked from rachelnabors/index.html
Last active March 2, 2023 14:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gaearon/779b12e05ffd5f51ffadd50b7ded5bc8 to your computer and use it in GitHub Desktop.
Save gaearon/779b12e05ffd5f51ffadd50b7ded5bc8 to your computer and use it in GitHub Desktop.
Adding multiple React components on a single HTML page
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Add Multiple React components on a single HTML page</title>
</head>
<body>
<h2>Add Multiple React components on a single HTML page</h2>
<p>This page demonstrates using React with no build tooling to add multiple components to a single page.</p>
<p>React is loaded as a script tag.</p>
<p>
This is the first comment.
<!-- We will put our React component inside this div. -->
<div class="like-button-root" data-commentid="1"></div>
</p>
<p>
This is the second comment.
<!-- We will put our React component inside this div. -->
<div class="like-button-root" data-commentid="2"></div>
</p>
<p>
This is the third comment.
<!-- We will put our React component inside this div. -->
<div class="like-button-root" data-commentid="3"></div>
</p>
<!-- Load React. -->
<!-- Note: when deploying, replace "development.js" with "production.min.js". -->
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
<!-- Load our React component. -->
<script src="like_button.js"></script>
</body>
</html>
'use strict';
function LikeButton(props) {
const [liked, setLiked] = React.useState(false);
if (liked) {
return 'You liked comment number ' + props.commentID;
}
return React.createElement(
'button',
{ onClick: () => setLiked(true) },
'Like'
);
}
// Find all DOM containers, and render Like buttons into them.
// Note we're using CSS classes instead of IDs so that we can find several nodes.
document.querySelectorAll('.like-button-root').forEach((rootNode) => {
const root = ReactDOM.createRoot(rootNode);
// Read the comment ID from a data-* attribute.
const commentID = parseInt(rootNode.dataset.commentid, 10);
root.render(
React.createElement(LikeButton, { commentID: commentID })
);
});
@mtd8899
Copy link

mtd8899 commented Oct 16, 2022

Hfh

@mtd8899
Copy link

mtd8899 commented Oct 16, 2022

Nbbn

@mtd8899
Copy link

mtd8899 commented Oct 16, 2022

Lnj

@mtd8899
Copy link

mtd8899 commented Oct 16, 2022

Ughj

@Ebrelus
Copy link

Ebrelus commented Oct 17, 2022

Learn about createElement here: create element
Learn about passing props here: passing props

@ian-lowe
Copy link

It would be helpful if <script src="like_button.js"></script> were changed to <script src="like-button.js"></script> for consistency with the new tutorial

@pratik9333
Copy link

Nice

@dmcshehan
Copy link

Cool!

@templar-ajay
Copy link

awesome, new documentation is really helpful

@kanikarungta
Copy link

kanikarungta commented Feb 19, 2023

can someone explain, why don't we need keys here for different LikeButton elements in line 24?

@kanikarungta
Copy link

oh, we are mounting them on different HTML nodes and not in same place, never mind!

@elvinhatamov
Copy link

I loved the new documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment