Show Menu
Cheatography

ReactJS Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Easy Install

create app
npm create­-re­act-app my-app
delete src
del src/*
creating files
type nul > index.css
type nul > index.js
index.js
import React from 'react'; 
import ReactDOM from 'react­-dom';
import './ind­ex.c­ss';

Build

run server
npm start
create bundle
npm run build

Deploy

 

Setup from Scratch

Initia­lizing
npm init
Install React
npm install react react-dom --save
Install webpack
npm install webpack webpac­k-d­ev-­server webpac­k-cli --save
Install babel
npm install babel-core babel-­loader babel-­pre­set-env babel-­pre­set­-react html-w­ebp­ack­-plugin --save-dev
 

Structure

index.html
type nul > index.html
App.js
type nul > App.js
main.js
type nul > main.js
webpac­k.c­onf­ig.js
type nul > webpac­kco­nfig.js
.babelrc
type nul > .babelrc

React

Set Compiler, Server & Loader

 File : webpack.config.js 

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
   entry: './main.js',
   output: {
      path: path.join(__dirname, '/bundle'),
      filename: 'index_bundle.js'
   },
   devServer: {
      inline: true,
      port: 8001
   },
   module: {
      rules: [
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   },
   plugins:[
      new HtmlWebpackPlugin({
         template: './index.html'
      })
   ]
}

packag­e.json

{
  "name": "reactApp",
  "version": "1.0.0",
  "description": "First React App",
  "main": "index.js",
  "scripts": {

/*"test": "echo \"Error: no test specified\" && exit 1" 
REMOVE THIS AND ADD FOLLOWING 2 LINES */ 

    "start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "KNiGHTblood",
  "license": "ISC",
  "dependencies": {
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "webpack": "^4.41.5",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.10.1"
  },
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.6",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "html-webpack-plugin": "^3.2.0"
  }
}

index.html

<!DOCTYPE html>
<html lang = "en">
   <head>
      <meta charset = "UTF-8">
      <title>React App</title>
   </head>
   <body>
      <div id = "app"></div>
      <script src = 'index_bundle.js'></script>
   </body>
</html>

App.js

import React, { Component } from 'react';
class App extends Component{
   render(){
      return(
         <div>
            <h1>Hello World</h1>
         </div>
      );
   }
}
export default App;

main.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';

ReactDOM.render(<App />, document.getElementById('app'));

.babelrc

{
   "presets":["env", "react"]
}
 

Compnents are made with jsx

HTML flips to js Like
<di­v> {'Hell­o'.t­oU­ppe­rCa­se()} </d­iv>
 
<div classN­ame­="he­llo­--u­ppe­r"> {'Hell­o'.t­oU­ppe­rCa­se()} </d­iv>