Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add React in One Minute
<!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>
'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');
const root = ReactDOM.createRoot(domContainer);
root.render(e(LikeButton));
@Attito-guilome
Copy link

Attito-guilome commented Apr 19, 2022

c'est vraiment génial

@hu1yang
Copy link

hu1yang commented Apr 21, 2022

thanks

@spicecoder
Copy link

spicecoder commented May 5, 2022

Has any one got a function component of Like Button,instead of class component, working in a straight HTML page ?

@andyhwong825
Copy link

andyhwong825 commented May 5, 2022

I don't think there's a way to get functional components to work. I used to use functional components until I started using React without JSX; I've totally switched over to using the class components.

@ArkURL
Copy link

ArkURL commented May 8, 2022

thanks for support while the tutorial of react cn was out-date🤣

@okobsamoht
Copy link

okobsamoht commented May 15, 2022

Hi all.
@spicecoder yes here is it for function component:

'use strict';

const e = React.createElement;

function LikeButton(props) {
    const [liked,setLiked] = React.useState(false)

    if (liked) {
      return 'You liked this.';
    }

    return e(
      'button',
      { onClick: () => setLiked(true) },
      'Like'
    );
}

const domContainer = document.querySelector('#like_button_container');
const root = ReactDOM.createRoot(domContainer);
root.render(e(LikeButton));

@andyhwong825 i always use function component. you can test the this code.

@okobsamoht
Copy link

okobsamoht commented May 15, 2022

@Bobinio please which version of REACT are you using?

@spicecoder
Copy link

spicecoder commented May 16, 2022

good one @okobsamoht , yes that's what I was thinking ,as long as we don't use JSX ,the dom rendring should able to handle both class and function components ; not sure why my first try did not work -any way thanks for confirming it does work.

@JVarghese99
Copy link

JVarghese99 commented May 17, 2022

HI,
Thank you for the example. I could get the example working as it is shown here.
In my case I have a prepackaged React component defined as JSX in project 1. I can do a production build of this project and import the js files in a website project. I am having trouble rendering the React component in the website project. The code in the website looks as follows

'use strict';

const btnContainer1 = document.querySelector('#button_container');
const btnroot = ReactDOM.createRoot(btnContainer1);

// this does not work. It gives console error "ExButton is not defined"
let element = <ExButton type="primary"></ExButton>;

// this works
// let element = <h1>Hello, world</h1>;
btnroot.render(element);

Any suggestion is appreciated.

@spicecoder
Copy link

spicecoder commented May 17, 2022

@JVarghese99 ,I don't see you are importing ExButton component - please do

@JVarghese99
Copy link

JVarghese99 commented May 18, 2022

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

@spicecoder
Copy link

spicecoder commented May 18, 2022

@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

@MerlinsServ
Copy link

MerlinsServ commented May 19, 2022

Ok

@jo777750
Copy link

jo777750 commented May 23, 2022

how to add a color to the 'You liked this' text?

@Dipsaint1
Copy link

Dipsaint1 commented May 25, 2022

Not working, I'm getting 'require' is not defined

@okobsamoht
Copy link

okobsamoht commented May 29, 2022

@jo777750
here it is:

if (this.state.liked) {
      return e(
          'span',
          {style:{color:"blue",background:"silver",fontSize:22}},
          'You liked this.'
          );
    }

Screenshot from 2022-05-29 15-28-53

@okobsamoht
Copy link

okobsamoht commented May 29, 2022

@Dipsaint1 can you show a screenshot?

@Dipsaint1
Copy link

Dipsaint1 commented May 29, 2022

@okobsamoht
Thank you for trying to help out.
I've found a solution.

@jigneshbhimani
Copy link

jigneshbhimani commented Jun 17, 2022

@gaearon How to pass props in button component?

@moccodes
Copy link

moccodes commented Jun 20, 2022

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.

@Chrissiku
Copy link

Chrissiku commented Jun 27, 2022

It's working

@jpol01
Copy link

jpol01 commented Jun 27, 2022

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

@MohamedAli00949
Copy link

MohamedAli00949 commented Jul 4, 2022

very nice, thank you

@lucia-2473
Copy link

lucia-2473 commented Jul 13, 2022

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).

@matfolio
Copy link

matfolio commented Aug 7, 2022

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.

@skillwork-mdimitrov
Copy link

skillwork-mdimitrov commented Aug 18, 2022

@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.

@skillwork-mdimitrov
Copy link

skillwork-mdimitrov commented Aug 18, 2022

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.

@gmumdzhiev
Copy link

gmumdzhiev commented Aug 29, 2022

Is it possible to implement the same logic , but for an react-native application ?

@melashu
Copy link

melashu commented Sep 6, 2022

Great

@olamiji-8
Copy link

olamiji-8 commented Oct 1, 2022

Wow this is nice

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