How do I use react.js without a bundler?

守給你的承諾、 提交于 2019-11-30 12:24:25

you have to make a distinction I think, one thing is the bundler (webpack or browserify), tools that takes the many files that compose your project and bundle them together in one or more files. These tools are not required but the are extremely useful if you have a larger project.

The other kind of tools that are often involved are transpiler, tools that takes your code and transform them in something else (like babel). If you are using plain old javascript they are not required, if you are using the new ES6 syntax you need them to translate your code in the old javascript and run it in every browser.

That said non of theese tools are required. the React tutorial includes the files directly in the page and it works without any other tools, it's not very performant but it works without other tools.

I've found this to be the easiest way to play with jsx, without any build steps:

<!DOCTYPE html>
<html>
<head>
    <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.14.0/babel.min.js"></script>
    <script type="text/babel" data-presets="es2017, stage-3" data-plugins="syntax-async-functions,transform-class-properties"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel" src="App.jsx"></script>
<script type="text/babel" >
    ReactDOM.render(<App/>, document.getElementById('app'));
</script>
</body>
</html>

and in App.jsx:

class App extends React.Component {

    render() {
        return <div>This is some jsx!</div>;
    }

}

In addition to what been said about webpack/browserify.

You can develop your application as a set of separate components and be able to run them directly without a need to bundle them into one file. To do that - build your components using es6 modules and import/export them where appropriate. Such components can be consumed by anyone the only thing users of your compoents should do is to use module loader and init it in the bootstrap of application.

Below I will give example with using systemjs as loader, but it can equally be requirejs or whatever.

YourComponent.jsx:

export class YourComponent extends React.Component {
  render() {
    return <div>Hi</div>;
  }
}

ReactDOM.render(<YourComponent/>, document.querySelector("#mount"));

It can be consumed if in index.html systemjs is setup like:

<!doctype html>
<html>
<body>
    <div id="mount"></div>

    <script src="lib/systemjs/dist/system.src.js"></script>

    <script>
        System.config({
            paths: {
                "react*": 'yourDistFolder/react/dist/react-with-addons', 
                "react-dom": 'yourDistFolder/react-dom/dist/react-dom'
            }             
        });

        System.defaultJSExtensions = true;
    </script>
<!--.....-->
    <script>
        System.import('yourcomponent').catch(function(e)
        {
            console.error(e);
        });
    </script>
</body>
</html>

In this sample I have included bootstraping and YourComponent is actually used as a root one, that is of course not necessary if it is part of parent application.

Hope this helps.

I guess in order to avoid using these tools you would just need to include react and react-dom into your page right before the closing body tag of your HTML doc, along with all your files. I believe the tutorial on the React website uses this approach and avoids using any tooling.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!