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));
@ozbcoz
Copy link

ozbcoz commented Nov 30, 2021

worked fine for me

@thesupermanny
Copy link

thesupermanny commented Jan 27, 2022

Worked well for me, thanks.

@BenMukebo
Copy link

BenMukebo commented Feb 22, 2022

Very nice thanks

@michaelHookon
Copy link

michaelHookon commented Mar 1, 2022

Very nice thanks

@steve4d
Copy link

steve4d commented Mar 2, 2022

For me, the HTML renders, but the React button does not. Chrome tools reports an error on line 3 of the js file:
const e = React.createElement; // Uncaught ReferenceError: React is not defined.
What am I missing?
I tried a couple things I found to add to babel.config.js and the js, but no change:

  1. {
    "presets": [
    "@babel/preset-env",
    ["@babel/preset-react", {"runtime": "automatic"}]
    ]
    }
  2. import React from 'react';

@smaster0517
Copy link

smaster0517 commented Mar 7, 2022

Hi,
Which version are you using?

This one works well.

I am sure it suits well the first learners.

@steve4d
Copy link

steve4d commented Mar 7, 2022

Thanks. I got the above example to work. Now, I want to convert it to Typescript. Do you have an example of how that could be done, possibly using Babel/Gulp in a build step?

@andyhwong825
Copy link

andyhwong825 commented Mar 10, 2022

Is it possible to use async/await via this methodology? Babel adds an import (babel-runtime/regenerator) to the first line of my transpiled javascript file, causing an error.

edit: I guess the better method would be to not use babel and just use React without jsx.

@Bobinio
Copy link

Bobinio commented Apr 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....

@007jwh
Copy link

007jwh commented Apr 13, 2022

nice

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

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