33

I started to look into ReactJS. It seems that Facebook just released version 15.0.1. I looked into this framework last year, during 0.12.x version, when it was using JSXTransformer, and now it seems it went away from it.

Now it seems that almost every tutorial suggests using latest React with Webpack. Is there a way not use webpack at all? I'm trying to find a good valid example with a grunt task for React 15.x.x.

Any help would be appreciated.

Midnight Coder
  • 721
  • 1
  • 8
  • 20
  • 4
    You don't need webpack. You don't even need JSX, you can just write `React.createElement` if you want. If you want JSX you need Babel, which will work fine with grunt. That's all you need. – azium Apr 13 '16 at 21:40
  • 1
    Have at it hoss: http://jamesknelson.com/learn-raw-react-no-jsx-flux-es6-webpack/ – lux Apr 14 '16 at 01:04
  • 8
    @nbrogi well it would be a pretty crappy answer to the question "How do I use React without Webpack?" to say "Just use webpack." – azium Aug 18 '16 at 15:35
  • @azium: since the solution is more pain than what it tries to solve, it's not such a stupid answer. Regardless, you can use Babel, Gulp, or whatever but there's no escaping the fact that React requires transpiling. – nkkollaw Aug 19 '16 at 07:26

6 Answers6

25

The simplest way:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">  
</head>

<body>
  <div id="root"></div>

  <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

  <script type="text/babel">

      class Hello extends React.Component{
        render() {
          return <p>Hello {this.props.name}</p>;
        };
      }

      ReactDOM.render(
        <Hello name='World'/>,
        document.getElementById('root'),
      );
  </script>
</body>

</html>
Reza
  • 455
  • 1
  • 5
  • 9
  • 3
    Is there a way to move the react code inside the script tag to an external file so it isn't all in one html file? – TomaszS Feb 26 '20 at 23:22
  • 2
    @TomaszS create a new file with the code then reference like this: `` – Dimitri W Aug 31 '20 at 05:54
  • Any ideas on how to fix this? `SyntaxError: unknown: Support for the experimental syntax 'classProperties' isn't currently enabled... Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.` when using functions inside the class? – Gianfranco P. Oct 23 '20 at 18:19
11

Jsx transformer is deprecated it seems.

Update 04/08/2019 : use https://github.com/developit/htm for a minimalist react setup.

If you don't want to use webpack, you have to think about which features you can live without.

With the advancement of browsers' support for ES6 features, you can now have ES6 syntax and also use modules (when activating experimental flags) without using Webpack.

If you want to use JSX, you will always need to transpile it to JS because there is no native support for it in browsers on the horizon. The most simple way of doing that is to add a Babel middleware with the "React" preset, to your development server.

Dependency management is also going to be complicated, because npm provides packages in CommonJS, which can't run as is in the browser...

If you want to experiment with that, you can check out an experimental React starter-kit that I've put on GitHub React Without Webpack that attempts to replicate most of webpack features using native browser features and http2.

Mister Fresh
  • 670
  • 1
  • 10
  • 22
  • [Snowpack](https://www.snowpack.dev/concepts/how-snowpack-works) looks like it will take npm packages and make them available via ESM imports. – Ian Dunn Jan 29 '21 at 20:56
8

The easiest way not to use webpack is to just use babel.

At the moment the most minimal set-up I can figure out is to use the babel-cli and babel-preset-react packages.

If you have a package.json file set up you just need to type:

npm install babel-cli babel-preset-env --save-dev

Then you need a .babelrc file with at least the following content: {"presets": ["react"]}.

If for example your javascript sources are in a js folder, you can compile them to a lib folder by adding a scripts field to your package.json like this:

"scripts": {
  "build": "babel js --out-dir lib"
},

So running npm run build compiles your javascript files to a lib folder.

Then you just need to link the compiled file in your html, like this:

<script src="lib/script.js"></script>

A minimal code to use react would be:

const HelloWorld = function HelloWorld(props, context) {
  return <p>Hello <strong>React</strong>!</p>;
};

ReactDOM.render(
  <HelloWorld />,
  document.getElementById('root'),
);

Notice that with this approach you are not transpiling es6 to es5, so if you want to support older browsers you still need to add the babel-preset-env package, and to modify the .babelrc file like this

{
  "presets": ["env", "react"]
}
Giorgio Tempesta
  • 1,816
  • 24
  • 32
  • 2
    This will not work if you use import statement because it will be converted to require statement which needs to be bundled using a bundler tool like webpack or Parcel or rollup. – sumanchalki Jul 20 '19 at 17:25
6

You just have to include the standalone version of babel, like it is explained in the docs: https://reactjs.org/docs/add-react-to-a-website.html#optional-try-react-with-jsx

Quickly Try JSX

The quickest way to try JSX in your project is to add this tag to your page:

Now you can use JSX in any tag by adding type="text/babel" attribute to it. Here is an example HTML file with JSX that you can download and play with.

This approach is fine for learning and creating simple demos. However, it makes your website slow and isn’t suitable for production. When you’re ready to move forward, remove this new tag and the type="text/babel" attributes you’ve added. Instead, in the next section you will set up a JSX preprocessor to convert all your tags automatically.

Here's an example:

index.html

<body>

    <div id="root"></div>

    <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

    <script type="text/babel">

        function Hello(props) {
            const [name, setName] = React.useState(props.name);
            return (
                <h1 onClick = {() => setName(Math.random().toString(36).substring(5))}>
                    Hello {name}
                </h1>
            );
        }

        ReactDOM.render(
            <Hello name='World'/>,
            document.getElementById('root'),
        );

    </script>

</body>    

danielrvt
  • 10,177
  • 20
  • 80
  • 121
1

You don't need Webpack or grunt, instead of JSXTransformer you need to use Babel https://reactjs.org/docs/getting-started.html#offline-transform

user2226755
  • 12,494
  • 5
  • 50
  • 73
Mikayel
  • 46
  • 2
1

You have to install react and reactDom and babel from CDN and if you need another library like axios you have to import it with script tag

the code below fetch a data and shows with h1 tag

<html>
  <head>
    <meta charset="utf-8" />
  </head>

  <body>
    <div id="root"></div>

    <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>


    <script src="https://unpkg.com/axios@0.24.0/dist/axios.min.js"></script>

    <script type="text/babel">
      function ShowPost(props) {
        const [post, setPost] = React.useState("");

        React.useEffect(()=>{
          
          const fetchData= async ()=>{
            const data = await axios.get(
              `https://jsonplaceholder.typicode.com/posts/${props.postId}`
            );
            setPost(data.data.body);
          }

          fetchData()

        },[])


        return <h1>{post}</h1>;
      }

      ReactDOM.render(<ShowPost postId={1} />, document.getElementById("root"));
    </script>
  </body>
</html>
Majid Gabrlo
  • 659
  • 6
  • 5