Some developers appear to think that setting up React for development is difficult. I believe that this is based on misinformation. In reality, setting up React isn't all that difficult.
Personally, I worry that a generalization about setting up and learning React is being created based upon the specifics of one particular type of setup (Webpack, Redux, ES6, JSX, Babel etc.), instead of a broad understanding of all of the setup options available.
In this article, I'm going to lay out seven React setups. The majority of the setups I'm going to showcase are, by and large, not all that difficult. However, "bringing your own architecture" (aka BYOA) to any situation is difficult. What do I mean by this? Since React aims to be a view only layer/library, it leaves an open architecture for you to fill in the pieces it does not provide. This openness can make setup difficult, but this isn't really the fault of React.
The short of it is that React itself is simple, but bringing your own architecture is not. Even BYOA can't fix the fact that browsers are missing a native JavaScript module loader/bundler. Keep this in mind, as the setups in this article move from simplistic to complex, aiming to fill the gap in the browser.
Obviously, each setup will only scratch the surface of possible configurations. Each setup could be, and should be, evolved to fit the unique particulars of the website or application you are building.
Before discussing the setups, if you are new to React, you should review these terminology I have provided at the end of this article.
Getting set up to run React code that will be rendered to the HTML DOM is dead simple if you've decided not to use JSX. You simply include the react.js
and react-dom.js
scripts into an HTML page and you are ready to write React code.
The react.js
file is the core React file needed to create React nodes and write React components. When you intend to render your components into an HTML document (i.e. the DOM), you'll also need the react-dom.js
file. The react-dom.js
file is dependent on the react.js
file and must be included after first including the react.js
file. An example of an HTML document properly including React is shown below.
With the react.js
file and react-dom.js
file loaded into an HTML page, it is then possible to create React nodes/components and then render them to the DOM. In the HTML below, a HelloMessage
React component is created containing a React <div>
node that is rendered to the DOM inside of the <div id="app"></div>
HTML element.
This setup is all you need to execute React code in ES5 browsers. However, this setup does not make use of JSX or ES 2015.
An additional script can be added to setup #1 that will allow for the usage of JSX/ES 2015. However, transforming JSX in the client using Babel is not a production-worthy solution. It places the burden of processing JSX/ES 2015 on the client at runtime. However, for non-production situations the browser.js
script from the Babel project can be added to an HTML document and provides the means to transform JSX in the client at runtime.
In the HTML file below the HelloMessage
component shown in setup #1 has been updated to use JSX.
The transformation of the code is occurring because we have included the browser.js
Babel file and given the <script>
element containing our React/JSX code a type
attribute of type="text/babel"
. When browser.js
is loaded, it will find any type="text/babel"
scripts and transform the script from JSX/ES 2015 to ES5 JavaScript. For example, the script contained in the following HTML document will be transformed to:
Note that the Babel project, as of Babel 6, no longer provides browser.js
to transform JSX code to ES5 code in the browser. Thus, you will have to use an older version of Babel (i.e. 5.8.23) that provides browser.js
for transforming JSX/ES* in the browser.
This is going to blow your mind - it did mine. SystemJS
, with help from the jspm CDN, will just sort out all of the details for React, JSX, and Babel (i.e. dynamic loading) - in the BROWSER AT RUNTIME!
All you have to do is serve the following HTML file:
Which imports the following a main.js
file:
And all of that just works. You don't have to install or download anything. You can try it in this plunker. It will even run locally if you use Firefox.
When the page loads it will acquire and install all the necessary dependencies - including Babel! Check out the source panel from Chrome devtools to see everything that is added.
The jspm CDN works similar to npmcdn.com. It sends source minified over HTTP/2 using depCache injnection, which might even make this approach suitable for production.
Now, you might be thinking this will only work with named jspm packages (i.e. pacakges defined in JSPM registry), but you'd be wrong. You can bypass jspm and also install packages directly from npm or GitHub that are not in the jspm registry. Of course, you have to tell jspm that you are doing this and the package will have to have a main file defined in package.json. For example, you can install the following packages using ES 2015 modules format (go ahead and try it in the plunker from above).
This setup is great for some quick development, but the potential for production use is still unknown given the use of futuristic tools like SPDY and HTTP/2.
Setup #1, #2, and #3 will work with online editors (e.g. jsbin or jsfiddle) when one needs to quickly set up a React environment and share React "pseudocode".
The fastest and easiest React setup with an online editor can be accomplished with JS Bin. Below, I demonstrate how easy it is to configure JS Bin for React coding.
This setup involves using the Babel-cli, Babel presets/plugins, and npm scripts to transform JSX/ES 2015 to ES5 code during development.
We'll create this setup in seven steps. Alternatively, you can follow the four steps below which use a GitHub repo to accelerate the setup.
npm install
from the cloned directoryIn this step, make sure you have installed or have the most recent stable version of Node.js and npm. Then run the following command to install browser-sync.
You may need to use "sudo" to install the package globally.
On your local file system create a directory with the following sub-directories and files.
Open the package.json
file and place the following empty JSON object inside of it:
Open a command prompt from the root of the directory you created in step 2. Then run the following npm commands:
and
Running these two commands will install the necessary npm packages for this setup. The project directory node_modules
folder should now contain the following npm packages:
Open the package.json
file which should look something like this:
Add the following Babel and scripts configurations to the package.json
file.
These updates configure Babel with the presets we installed from npm and provide two "scripts"
that we can run using the npm cli.
Open the index.html
file and copy the following HTML into the file:
Note that we are pulling react.js
and react-dom.js
from the node_modules
directory.
Open the app.js
file and copy the following JavaScript into the file:
From the root of the setup directory, open a command prompt and run the following npm command
Next, open another new command prompt and run the following npm command
Both of these commands will continue to run while developing.
If you followed all the steps correctly, Browsersync should have opened a browser running the index.html
file and app.js
file at http://localhost:4000. Both Babel and Browsersync have been configured to re-run when changes are made.
This setup does not assume that you want to build a SPA and only assumes you want to create HTML pages that make use of React, JSX, and ES 2015.
This setup involves using Webpack and several loaders to transform JSX/ES 2015 to ES5 code. By using Webpack, JavaScript modules can be loaded using the ES 2015 module format (commonJS behind the scenes), properly transformed, ad then bundled.
We'll create this setup in seven steps. Alternatively, you can follow the four steps below which use a GitHub repo to accelerate this setup.
npm install
from the cloned directoryIn this step, make sure you have installed or have the most recent stable version of Node.js and npm. Then run the following command to install Webpack and browser-sync globally.
You may need to use "sudo" to install the packages globally.
On your local file system, create a directory with the following sub-directories and files.
Open the package.json
file and place the following empty JSON object inside of it:
Open a command prompt from the root of the directory you created in step 2. Then run the following npm commands:
Next run:
Running these two commands will install the necessary npm packages for this setup. The project directory node_modules
folder should now contain the following npm packages:
Open app.js
and add the following to the file:
Open app.css
and add the following to the file:
Open math.js and add the following to the file:
Open index.html
and add the following to the file:
Open webpack.config.js and add the following to the file:
Open the package.json file which should look something like this:
Add the following scripts configurations to the package.json
file.
This update provides two "scripts"
we can run using the npm cli.
From the root of the setup directory, open a command prompt and run the following npm command:
Next, open another new command prompt and run the following npm command:
Both of these commands will continue to run while developing.
If you followed all the steps correctly, Browsersync should have opened a browser running the index.html
file and app.js
file at http://localhost:4000. Both Webpack and Browsersync have been configured to re-run when changes are made.
This setup is just the tip of the iceberg. Depending upon the scope and scale of the application you are building, this basic Webpack setup could be configured and reconfigured in many ways. Start with this basic setup, study Webpack in-depth, and slowly scale it from here. Be very careful about the complexity you add to this setup, as you risk unintentionally creating a house of cards.
This React setup involves using systemJS/jspm-cli to tranform (JSX/ES 2015), load, and bundle JavaScript modules (and CSS) using the ES 2015 module format.
I think we have saved the best for last. Mostly because systemJS/jspm handles the configuration file with a cli tool and the solution would appear to be the most future proof offering available today.
We'll create this setup in nine steps. Alternatively, you can follow the four steps below which use a GitHub repo to speed up this setup.
npm install && jspm install
from the cloned directoryIn this step, make sure you have installed or have the most recent stable version of Node.js and npm. Then run the following command to install jspm and browser-sync globally.
On your local file system create a directory with the following sub-directories and files.
Open the package.json
file and place the following empty JSON object inside of it:
Open a command prompt from the root of the directory that you created in step 2. Run the following npm commands:
Running this command will install the necessary npm packages for this setup. The project directory node_modules
folder should now contain the following npm packages:
Open a command prompt from the root of the directory you created in step 2. Then run the following jspm-cli commands:
> jspm init
This will ask you 9 questions, just hit enter for each question.
This will create a config.js
and jspm_packagees
directory (with default packages) for you. The setup directory should look like this:
Open config.js
and change the babelOptions
object from:
to:
Open app.js
and add the following to the file:
Open app.css
and add the following to the file:
Open math.js
and add the following to the file:
Open index.html
and add the following to the file:
Open a command prompt from the root of the directory you created in step 2. Then run the following jspm-cli command:
> jspm install react react-dom css npm:@telerik/kendo-react-buttons
This might confuse some people, so let me clarify that by using jspm you are now installing jspm, npm, and GitHub packages using the jspm-cli and not the npm command line tool.
The above command will install React, react-dom, a jspm css plugin, and the Kendo UI React buttons in the jspm_packages
folder. These dependencies are documented automatically in the package.json file. Additionally, the jspm configuration file is updated so that the installed packages can be used without having to manually update the config.js
file.
The updated jspm_packages
folder will now look like this:
Open the package.json file which should look something like this:
Add the following scripts configurations to the package.json
file.
This update provides two "scripts"
we can run using the npm cli.
From the root of the setup directory open a command prompt and run the following npm command:
If you followed all the steps correctly Browsersync should have open a browser running the index.html
file and app.js
file at http://localhost:4000. Browsersync has been configured to re-run when changes are made.
SystemJS/jspm offers a bundled mode. From the root of the setup directory open a command prompt and run the following npm command:
By running this command, the browser should reload and be running from a build.js
file that has been created for you in the root of the setup directory. Additionally, the bundling process will combine and in-line into the HTML document any CSS that was imported in a module (e.g. app.css
)
To unbundle simply run:
Hopefully, one of these seven setups will get you going with React without issues. I'd resist the urge to go and grab a large React boilerplate or starter kit unless the setup is as simple as the ones found here. When dealing with a BYOA situation, always start small and question every layer of complexity.
Now go use React, the setup should not be blocking you.
Related Resources
Babel transforms JavaScript ES* (i.e. JS 2016, 2016, 2017) to ES5. Babel is the tool of choice from the React team for writing future ES* and transforming JSX to ES5 code.
Babel comes with a CLI tool, called Babel CLI, that can be used to compile files from the command line.
"The Document Object Model (DOM) is a programming interface for HTML, XML and SVG documents. It provides a structured representation of the document as a tree. The DOM defines methods that allow access to the tree, so that they can change the document structure, style and content. The DOM provides a representation of the document as a structured group of nodes and objects, possessing various properties and methods. Nodes can also have event handlers attached to them, and once an event is triggered, the event handlers get executed. Essentially, it connects web pages to scripts or programming languages." - MSD
The 5th edition of the ECMAScript standard. The ECMAScript 5.1 edition was finalized on June 2011.
The 6th edition of the ECMAScript standard. AKA JavaScript 2015. The ECMAScript 6th edition was finalized on June 2015.
Name of the specification that will provide updates to the JavaScript language in 2016.
Used to represent the current version of JavaScript, as well as potential future versions that can be written today using tools like Babel. When you see "ES*" it more than likely means you'll find uses of ES5, ES6, and ES7 together.
jspm is a package manager for the SystemJS universal module loader. It can Load any module format (ES6, AMD, CommonJS and globals) directly from any registry such as npm and GitHub with flat version-ed dependency management.
JSX is an optional XML-like syntax extension to ECMAScript that can be used to define an HTML-like tree structure in a JavaScript file. The JSX expressions in a JavaScript file must be transformed to JavaScript syntax before a JavaScript engine can parse the file. Babel is typically used, and recommended for transforming JSX expressions.
An open-source, cross-platform runtime environment for writing JavaScript. The runtime environment interprets JavaScript using Google's V8 JavaScript engine.
The package manager for JavaScript born from the Node.js community.
Module loader that loads, ES6 modules, AMD, CommonJS and global scripts in the browser and NodeJS. Works with both Traceur, Babel, and Typescript.
A module loader and bundler that takes modules (.js, .css, .txt, etc...) with dependencies and generates static assets representing those modules.
Header image courtesy of www.david baxendale.com
Cody Lindley is a front-end developer working as a developer advocate for Telerik focused on the Kendo UI tools. He lives in Boise, ID with his wife and three children. You can read more about Cody on his site or follow him on Twitter at @codylindley.