Skip to main content

React Ecosystem Setup — Step-By-Step Walkthrough

React Ecosystem Setup — Step-By-Step Walkthrough

Initial Configuration

project_root
|
|---dist
| |
| |---app.html
|
|---package.json
|---server.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>React - Basic Setup</title>
</head>
<body>
<div id="container">React</div>
</body>
</html>

Basic Express Server

const express = require('express');
const path = require('path');
const app = express();
const port = process.env.PORT || 8080;
app.set('port', port);
app.use(express.static(path.join(__dirname, './dist')));
app.get('*', (req, res) => {
console.log('Serving ', req.url);
res.sendFile(__dirname + '/dist/app.html');
});
app.listen(port, () => console.log('Listening on port', port));

Webpack

  • It’ll resolve our dependencies by going into the modules we import and concatenating those, pulling them out of our node_modules and putting them at the top of our single file. It’ll recursively go through every dependency and the dependencies’ dependencies, resolving all the way down.
  • It’ll transpile our farm-fresh, cutting-edge ES6+ down to ES5 so that it’ll work on virtually every browser.
  • It’ll minify our code by removing whitespace and shortening variable names, reducing the final file size.

Getting Started

console.log('Testing our bundle');
npm install -D webpack
project_root
|
|---dist
| |
| |---app.html
|
|---src
| |
| |---index.js
|
|---package.json
|---package-lock.json
|---server.js
|---webpack.config.js
const path = require('path');module.exports = {
entry: path.resolve(__dirname, './src/index.js'),
output: {
path: path.resolve(__dirname, './dist'),
filename: 'app.bundle.js',
}
};
webpack
node dist/app.bundle.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>React - Basic Setup</title>
</head>
<body>
<div id="container">React</div>
</body>
<script src="app.bundle.js"></script>
</html>

Flags

webpack -d
webpack -d --watch

Babel

npm install -D babel-core babel-loader babel-preset-env
  • Babel-core is a library that takes in code as a string and transpiles it down.
  • Babel-loader is what allows us to tie babel-core into webpack and lets webpack’s configuration know to use the babel-core library.
  • Babel-preset-env is essentially a configuration for babel which lets us pass in options. Without the options, it uses defaults, which means it will understand code up to ES2017 standards and transpile it down to ES5.
const path = require('path');module.exports = {
entry: path.resolve(__dirname, './src/index.js'),
output: {
path: path.resolve(__dirname, './dist'),
filename: 'app.bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: [/node_modules/],
use: {
loader: 'babel-loader',
options: {
presets: ['env']
}
}
}
]
}

};

React

npm install react react-dom
npm install -D babel-preset-react
const path = require('path');module.exports = {
entry: path.resolve(__dirname, './src/index.js'),
output: {
path: path.resolve(__dirname, './dist'),
filename: 'app.bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: [/node_modules/],
use: {
loader: 'babel-loader',
options: {
presets: ['react', 'env']
}
}
}
]
}
};

Our First React Component

import React from 'react';
import ReactDOM from 'react-dom';
const container = document.getElementById('container');
const myDiv = <div>My First React Component!</div>;
ReactDOM.render(myDiv, container);
  • We can use import statements because webpack will resolve those for us. It’ll grab React and ReactDOM out of our node_modules and throw them at the top of our bundle, with the rest of our code below it.
  • We’re accessing the div with ID container for rendering our component. This is the DOM element that we’ll place our React elements in.
  • The next line, creating the variable myDiv, is the most exciting. We’re writing something that looks like HTML in our Javascript. myDiv is a variable that contains what we can think of as a DOM element that we just created.
  • The last line uses the ReactDOM library to render our myDiv element on the DOM.
'use strict';
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var container = document.getElementById('container');
var myDiv = _react2.default.createElement(
'div',
null,
'My First React Component!'
);
_reactDom2.default.render(myDiv, container);

JSX

const out = 'outer';
const inn = 'inner';
const div = 'Div';
<div className={out + div}>
<div className={inn + div}>
<h1>JSX</h1>
<span>Creating a JSX Div</span>
</div>
</div>
'use strict';var out = 'outer';
var inn = 'inner';
var div = 'Div';
React.createElement(
'div',
{ className: out + div },
React.createElement(
'div',
{ className: inn + div },
React.createElement(
'h1',
null,
'JSX'
),
React.createElement(
'span',
null,
'Creating a JSX Div'
)
)
);
<div class="outerDiv">
<div class="innerDiv">
<h1>JSX</h1>
<span>Creating a JSX Div</span>
</div>
</div>

Refactoring the Component

project_root
|
|---dist
| |
| |---app.html
|
|---src
| |
| |
| |---components
| | |
| | |---app.js
| |
| |---index.js
|
|---package.json
|---package-lock.json
|---server.js
|---webpack.config.js
import React from 'react';
import ReactDOM from 'react-dom';
const container = document.getElementById('container');
const app = (
<div>
<h1>JSX</h1>
<span>My first JSX span!</span>
</div>
);
ReactDOM.render(app, container);
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/app';
const container = document.getElementById('container');ReactDOM.render(App, container);
import React from 'react';export default (
<div>
<h1>JSX</h1>
<span>My first JSX span!</span>
</div>
);

Exporting a Component

import React from 'react';export default function app() {
return
(
<div>
<h1>JSX</h1>
<span>My first JSX span!</span>
</div>
);
}
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/app';
const container = document.getElementById('container');ReactDOM.render(<App></App>, container);
<div></div> === <div />
<App></App> === <App />

CSS

npm install -D style-loader css-loader
const path = require('path');module.exports = {
entry: path.resolve(__dirname, './src/index.js'),
output: {
path: path.resolve(__dirname, './dist'),
filename: 'app.bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: [/node_modules/],
use: {
loader: 'babel-loader',
options: {
presets: ['react', 'env']
}
}
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
}

]
}
};
project_root
|
|---dist
| |
| |---app.html
|
|---src
| |
| |---index.js
| |
| |---components
| | |
| | |---app.js
| |
| |---styles
| |
| |---app.css
| |---index.css
|
|---package.json
|---package-lock.json
|---server.js
|---webpack.config.js
html, body {
font-family: sans-serif;
background-color: #dddddd;
}
h1 {
color: #31416D;
}
...
import App from './components/app';
import './styles/index.css';
...
import React from 'react';
import '../styles/app.css';
...

Comments

Popular Posts

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...

16 AWS Gotchas

16 AWS Gotchas In January I launched the MVP for my own startup,  Proximistyle , which helps you find what you’re looking for nearby. On advice from friends and industry contacts I chose AWS as my cloud provider. Having never had to set up my own cloud infrastructure before, the learning curve to get from no experience to a stable VPC system I was happy with was significantly steeper than expected, and had its fair share of surprises. #1 Take advantage of the free resources offered AWS offers a free tier for new accounts. If you have recently bought a domain and set up a company you qualify for the free tier for a year. Additionally, if you are a bootstrapped startup you can apply for  the Startup Builders package  and get $1000 in AWS credits. After doing the above, you’re now ready to get started with setting up the AWS infrastructure for your startup. #2 Set up billing budgets and alerting The very first thing you should do after setting up billing, is enabling a budge...

Ultimate Folder Structure For Your React Native Project

  Ultimate Folder Structure For Your React Native Project React native project structure React Native is a flexible framework, giving developers the freedom to choose their code structure. However, this can be a double-edged sword for beginners. Though it offers ease of coding, it can soon become challenging to manage as your project expands. Thus, a structured folder system can be beneficial in many ways like better organization, simplified module management, adhering to coding practices, and giving a professional touch to your project. This write-up discusses a version of a folder arrangement that I employ in my React Native projects. This structure is based on best practices and can be modified to suit the specific needs of your project. Before we get into the project structure let’s give credit to @sanjay who has the original idea of the structure but I modify his version of the code, to make it better. Base library axios  — For network calling. react-navigation ...