How can you set up a React application manually?

How can I set up a React application manually? I don't want to use create-react-app, because I want to understand the core concepts.

How can you set up a React application manually?

You can use a bundler (e.g. Webpack) yourself. Let me walk you through this in a few steps. If you want to read a whole tutorial about it, check out this one.

Create Project

  1. mkdir minimal-react-boilerplate 
  2. cd minimal-react-boilerplate 
  3. npm init -y 

Create a Folder for your Build

  1. mkdir dist 
  2. cd dist 
  3. touch index.html 
In the index.html file, paste the following boilerplate HTML:
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <title>The Minimal React Webpack Babel Setup</title> 
  5. </head> 
  6. <body> 
  7. <div id="app"></div> 
  8. <script src="./bundle.js"></script> 
  9. </body> 
  10. </html> 
Two important things:

1) The bundle.js will be all our React/JavaScript code bundled by Webpack.

2) The id=”app” is where React hooks into our HTML to render all components.

That’s everything we need.

1) Webpack

Since we created our project as npm project (npm init -y), we can install node packages via npm. Install the following ones for Webpack:

  1. npm install --save-dev webpack webpack-dev-server webpack-cli 

In your package.json, add the following npm script to start our application:

  1. ... 
  2. "scripts": { 
  3. "start": "webpack-dev-server --config ./webpack.config.js --mode development", 
  4. ... 
  5. }, 
  6. ... 

Also add a Webpack configuration to your project as it is used in the npm script from before:

  1. touch webpack.config.js 

Put in the following config:

  1. module.exports = { 
  2. entry: './src/index.js', 
  3. output: { 
  4. path: __dirname + '/dist', 
  5. publicPath: '/', 
  6. filename: 'bundle.js' 
  7. }, 
  8. devServer: { 
  9. contentBase: './dist' 
  10. } 
  11. }; 

Roughly the configuration file says that (1) we want to use the src/index.js file as entry point to bundle all of its imported files. (2) The bundled files will result in a bundle.js file which (3) will be generated in our already set up /dist folder. The /dist folder will be used to serve our app.

Source Code

Let’s add some source code to see whether Webpack works to bundle this source code:

  1. mkdir src 
  2. cd src 
  3. touch index.js 

In the new index.js file, add the following code:

  1. console.log('My Minimal React Webpack Babel Setup'); 

With npm start you should be able to start your application, to open the browser, and to see the console.log in your developer tools.

Babel

Babel enables you writing your code in with JavaScript which isn’t supported yet in most browser. Perhaphs you have heard about JavaScript ES6 (ES2015) and beyond. With Babel the code will get transpiled back to vanilla JavaScript so that every browser, without having all JavaScript ES6 and beyond features implemented, can interpret it. In order to get Babel working with all this and React, you need to install all dependencies.

  1. npm install --save-dev @babel/core @babel/preset-env babel-loader @babel/preset-react 

In your package.json, add all the new Babel configuration:

  1. ... 
  2. "keywords": [], 
  3. "author": "", 
  4. "license": "ISC", 
  5. "babel": { 
  6. "presets": [ 
  7. "@babel/preset-env", 
  8. "@babel/preset-react" 
  9. ] 
  10. }, 
  11. "devDependencies": { 
  12. ... 

In your webpack.config.js, let Webpack know how to transpile JavaScript and JSX (React’s Syntax):

  1. module.exports = { 
  2. entry: './src/index.js', 
  3. module: { 
  4. rules: [ 
  5. { 
  6. test: /\.(js|jsx)$/, 
  7. exclude: /node_modules/, 
  8. use: ['babel-loader'] 
  9. } 
  10. ] 
  11. }, 
  12. resolve: { 
  13. extensions: ['*', '.js', '.jsx'] 
  14. }, 
  15. output: { 
  16. path: __dirname + '/dist', 
  17. publicPath: '/', 
  18. filename: 'bundle.js' 
  19. }, 
  20. devServer: { 
  21. contentBase: './dist' 
  22. } 
  23. }; 

Setup React

Finally, let’s install React:

  1. npm install --save react react-dom 

In your src/index.js file, add the following code:

  1. import React from 'react'; 
  2. import ReactDOM from 'react-dom'; 
  3.  
  4. const title = 'My Minimal React Webpack Babel Setup'; 
  5.  
  6. ReactDOM.render( 
  7. <div>{title}</div>, 
  8. document.getElementById('app') 
  9. ); 

Note that we are using the app identifier here that was defined in our dist/index.html file before. Finally start the application again with npm start and you should see the React output.

That’s it for a very concise walkthrough. As I said before, if you need more explanation, better syntax highlighting, or more advanced tools.

Post a Comment

Previous Post Next Post