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

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...

An introduction to Size Classes for Xcode 8

Introduction to Size Classes for Xcode In iOS 8, Apple introduced  size classes , a way to describe any device in any orientation. Size classes rely heavily on auto layout. Until iOS 8, you could escape auto layout. IN iOS8, Apple changed several UIKit classes to depend on size classes. Modal views, popovers, split views, and image assets directly use size classes to determine how to display an image. Identical code to present a popover on an iPad  causes a iPhone to present a modal view. Different Size Classes There are two sizes for size classes:  compact , and  regular . Sometime you’ll hear about any.  Any  is the generic size that works with anything. The default Xcode layout, is  width:any height:any . This layout is for all cases. The Horizontal and vertical dimensions are called  traits , and can be accessed in code from an instance of  UITraitCollection . The  compact  size descr...

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