-
-
Save gaearon/6668a1f6986742109c00a581ce704605 to your computer and use it in GitHub Desktop.
<!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@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> |
HI @spicecoder : thank you. I did try importing the ExtButton component in the above javascript but is gave error "require is not defined". I manged to get my example working by exporting the ExtButton in the reactjs project (window as any).ExButton = ExButton
Please see my complete example here https://dev.to/jvarghese/how-to-use-previously-packaged-reactjs-component-in-static-website-21be
@JVarghese99 ,firstly you need to make sure you can generate the component i.e you need to have the full stack of dependent components ,e.g your button component looks like a custom component as well ; once you can generate the component in a reactjs project, then build that project & you should have the right code along with the mimified js that you can bring to your static site
Ok
how to add a color to the 'You liked this' text?
Not working, I'm getting 'require' is not defined
@jo777750
here it is:
if (this.state.liked) {
return e(
'span',
{style:{color:"blue",background:"silver",fontSize:22}},
'You liked this.'
);
}
@Dipsaint1 can you show a screenshot?
@okobsamoht
Thank you for trying to help out.
I've found a solution.
@gaearon How to pass props in button component?
Hi I have resolved this bug, make sure the file name matches the React Component so e.g
<script src="like_button.js"></script>
file name in script must be like_button.js_ they must match and like button will appear
Thank you.
It's working
If I use the three lines of code from this page:
https://reactjs.org/docs/add-react-to-a-website.html
const domContainer = document.querySelector('#like_button_container'); const root = ReactDOM.createRoot(domContainer); root.render(e(LikeButton));
The LIKE button does not appear on the html page.
But if I use just the last two lines in the downloaded js file the LIKE button does appear:
const domContainer = document.querySelector('#like_button_container'); ReactDOM.render(e(LikeButton), domContainer);
I don't know enough js/react to know what the above 3 lines does not work but the 2 lines does.
It is rather troubling especially when I am brand new to react and I am told that the react docs are the best place to learn react and I have flunked on the very first item....
same here i couldnt make it work, weird situation
very nice, thank you
Thank you for this.
Is there a way to re-render the React component based on some action on the HTML page?
For example, changing a date filter and re-rendering the React component with new values for the props (the dates to filter by).
If I use the three lines of code from this page:
https://reactjs.org/docs/add-react-to-a-website.html
const domContainer = document.querySelector('#like_button_container'); const root = ReactDOM.createRoot(domContainer); root.render(e(LikeButton));
The LIKE button does not appear on the html page.
But if I use just the last two lines in the downloaded js file the LIKE button does appear:
const domContainer = document.querySelector('#like_button_container'); ReactDOM.render(e(LikeButton), domContainer);
I don't know enough js/react to know what the above 3 lines does not work but the 2 lines does.
It is rather troubling especially when I am brand new to react and I am told that the react docs are the best place to learn react and I have flunked on the very first item....same here i couldnt make it work, weird situation
Unzip the downloaded file and open its contents using an IDE of your choice. You can use a live server to publish the content of your html file. The problem is that the script tag is not able to locate the referenced script file.
@gaearon How to pass props in button component?
Try out https://github.com/michaldoda/react-supervisor#readme. Haven't personally tested it, but looks promising.
Thank you for this. Is there a way to re-render the React component based on some action on the HTML page? For example, changing a date filter and re-rendering the React component with new values for the props (the dates to filter by).
At the moment I'm looking into https://github.com/michaldoda/react-supervisor#what-it-does. Seems like it might do what you desire, you can give it a shot.
Is it possible to implement the same logic , but for an react-native application ?
Great
Wow this is nice
Really helpful thanks alot
Is it possible to implement the same logic , but for an react-native application ?
yes I think so
I have a question guys, what if I created another component and I want to call it or import it inside like_button.js how to do that?
I tried to import a component using react cdn but I cand import it, I posted the problem in stack overflow. https://stackoverflow.com/questions/75375157/how-to-import-my-component-using-reactjs-cdn?noredirect=1#comment133002007_75375157
Great
thanks
jsfiddle demo but without the js url component but direct inside the javascript:
https://jsfiddle.net/onigetoc/ua2hoqm1/
@JVarghese99 ,I don't see you are importing ExButton component - please do