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

Reloading UITableView while Animating Scroll in iOS 11

Reloading UITableView while Animating Scroll Calling  reloadData  on  UITableView  may not be the most efficient way to update your cells, but sometimes it’s easier to ensure the data you are storing is in sync with what your  UITableView  is showing. In iOS 10  reloadData  could be called at any time and it would not affect the scrolling UI of  UITableView . However, in iOS 11 calling  reloadData  while your  UITableView  is animating scrolling causes the  UITableView  to stop its scroll animation and not complete. We noticed this is only true for scroll animations triggered via one of the  UITableView  methods (such as  scrollToRow(at:at:animated:) ) and not for scroll animations caused by user interaction. This can be an issue when server responses trigger a  reloadData  call since they can happen at any moment, possibly when scroll animation is occurring. Example of s...

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...

Xcode & Instruments: Measuring Launch time, CPU Usage, Memory Leaks, Energy Impact and Frame Rate

When you’re developing applications for modern mobile devices, it’s vital that you consider the performance footprint that it has on older devices and in less than ideal network conditions. Fortunately Apple provides several powerful tools that enable Engineers to measure, investigate and understand the different performance characteristics of an application running on an iOS device. Recently I spent some time with these tools working to better understand the performance characteristics of an eCommerce application and finding ways that we can optimise the experience for our users. We realised that applications that are increasingly performance intensive, consume excessive amounts of memory, drain battery life and feel uncomfortably slow are less likely to retain users. With the release of iOS 12.0 it’s easier than ever for users to find applications that are consuming the most of their device’s finite amount of resources. Users can now make informed decisions abou...