React is a front-end-component rendering framework by Facebook.[116] Teaching how to construct UIs with React is outside the scope of this book, but here you’ll learn how to wire it into a webpack-built Node.js project. To learn to use React, I recommend stepping through the Tic-Tac-Toe tutorial.[117]
Fortunately, it doesn’t take much code to get started using React, though you will need a transpiler. React typically relies on transpiling with Babel, but here we’ll use TypeScript to maintain consistency with the rest of the UI code in the book. The differences between React and TypeScript are summarized in Transpiling with TypeScript.
In the code downloads that accompany this book, you’ll find a directory called extra/react-webpack. This directory contains a very simple React project that builds with webpack and TypeScript.
Like other webpack projects in this book, the react-webpack project runs with webpack-dev-server through npm. You can start it up and take a look if you’re interested in poking around.
| | $ cd react-webpack |
| | $ npm start |
| | |
| | > react-webpack@1.0.0 start ./extra/react-webpack |
| | > webpack-dev-server |
| | |
| | Project is running at http://localhost:61200/ |
| | webpack output is served from / |
| | Content not from webpack is served from ./extra/react-webpack/dist |
| | ts-loader: Using typescript@2.3.2 and ./extra/react-webpack/tsconfig.json |
| | Hash: 810e5499eb5e676ce562 |
| | Version: webpack 2.4.1 |
This is the simplest React project I could come up with that builds with webpack. You can use it as a reference implementation to compare against, or as a skeleton project rather than setting everything up yourself.
Here’s a listing of that project’s contents.
| | $ cd react-webpack |
| | $ tree -I node_modules --dirsfirst |
| | . |
| | ├── src |
| | │ ├── index.html |
| | │ ├── index.tsx |
| | │ └── vendor.ts |
| | ├── package.json |
| | ├── package-lock.json |
| | ├── tsconfig.json |
| | └── webpack.config.js |
Notice the now-familiar package.json, tsconfig.json, and webpack.config.json files. We’ll discuss these in a bit, but first let’s look at the src, which contains the project source code files (as per React’s convention).
The src/vendor.ts file includes links to Bootstrap’s CSS and JavaScript code. That way webpack will create a separate bundle file for those assets. For more on how this works, see webpack’s documentation on code splitting.[118]
Next, notice the index.tsx. This is the main entry point of the application. It pulls together the React components and injects them into an element defined in the index.html file. index.html is used as a base template for HtmlWebpackPlugin to load the main application component.
The reason for tsx instead of just ts is that React uses JSX to allow HTML literal content inside of JavaScript files.[119] TSX is the TypeScript flavor of JSX.
To build a React-based project using TypeScript, here are the packages you’ll need to add to your project:
Since React is developed in regular JavaScript, the TypeScript typings are implemented separately and distributed under the @types/ prefix.
For webpack, you’ll need the familiar cohort of packages:
tsconfig.json doesn’t need anything special to support React above and beyond what you used in Chapter 8, Creating a Beautiful User Experience. However, webpack.config.js requires one minor tweak. In the rules section, the test for ts files needs to be extended to match tsx files as well.
| | rules: [{ |
| » | test: /\.tsx?$/, |
| | loader: 'ts-loader', |
| | },{ |
With those changes out of the way, you should be in a good spot to use React to develop the front end for your own Node.js projects. Best of luck!