- React.js is an open-source, front end, JavaScript
library
for building user interfaces or UI components. - It is maintained by
Facebook
and a community of individual developers and companies. - React is only concerned with
state management
andrendering
that state to theDOM
. - React is called
library
instead offramework
because it only supportsDOM manipulations
.- Other UI framework capabilities like routing, two-way-data-binding will be handled by other libraries.
- React is a better fit for developing
SPA (Single-Page Application)
.- These apps work inside a browser and does not require page reloading during use like Gmail, Facebook, Instagram etc...
- These are great for working in teams because the backend developers can just focus on the API, and frontend developers can focus on creating the best user experience.
- Major challenge faced during complex UI codebase is involvement of massive team members.
- More time is spent in determining what code does what. React solves this with below:
- The
DOM
is a Tree representation of apage
, starting from the tag, going down into every child, which are called nodes.- Every time the DOM changes, browser does two intensive operations:
- repaint: visbile changes to element but do not affect its layout.
- reflow: changes that affect the layout of a portion/whole page. These are critical to perform.
- Every time the DOM changes, browser does two intensive operations:
- To solve this problem in React, Complexity of UI code is addressed by break down application into smaller, single-purpose
Components
. - Each component maintains a
State
. Based on user interactions, React re-renders components by reacting to change in state. - During re-rendering, once the component is ready to render, a virtual snapshot of the change in component state is taken.
- React now compares the snapshot with the real
DOM (Document Object Model)
and analyzes the differences. Once it finds the difference, the changes are applied to the UI that’s reflected on the user’s screen. - React uses Virtual DOM (
VDOM
) process to update actual DOM. This has following steps:- When a component state changes, React marks that Component as
dirty
. - Create a
VDOM
with anew state
. - Compare it with older VDOM.
- Update only different nodes in real DOM.
- Assign new VDOM as an older VDOM.
- When a component state changes, React marks that Component as
- Video - How VDOM works
- The
-
React installation require below:
- React
- Core:
React
is a javascript library, designed for building better user interfaces.- It is used to provide
component
,classes
,createelement
, etc.
- DOM:
React-DOM
is a complimentary library to React which glues React to the browser DOM.- It provides methods like
render()
orfindDOMNode()
- Core:
- Babel 7.9
- It is used for latest ECMAScript and JSX syntax support from React 17.
- Below package will be used
babel-core:
The main engine of babel plugin.babel-preset-env:
ECMAScript support.babel-preset-react:
Compiles JSX syntax to javascript.babel-loader:
Used to bridge communication between Webpack and Babel.
- Webpack 5
- It is most widely used and an accepted
module bundler
andtask runner
throughout React.js community. - Below package will be used
webpack:
The main engine of webpack plugin.webpack-cli:
Allow to access webpack commands through CLI.webpack-dev-server:
A minimal server for client-side development.html-webpack-plugin:
Generate a html file and hooks withindex.js
file.html-loader:
Export html as string to be read by webpack.
- It is most widely used and an accepted
- React
-
Create React Web Application using CDN
-
HelloWorld React App steps
# Initialize NPM Project npm init -y # Required during Runtime npm i -S react react-dom # Required during Development-only npm i -D @babel/core @babel/preset-env @babel/preset-react babel-loader npm i -D webpack webpack-cli webpack-dev-server html-webpack-plugin npm i -D html-loader # Babel Configuration vi .babelrc { "presets": [ "@babel/preset-env", [ "@babel/preset-react", { "runtime": "automatic" } ] ] } # Webpack Configuration vi webpack.config.js const path = require('path'); const HTMLWebpackPlugin = require('html-webpack-plugin'); module.exports = { mode: 'development', entry: path.resolve(__dirname, './src/index.js'), module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: [ { loader:'babel-loader' } ] }, { test: /\.html$/i, use: [ { loader: 'html-loader' } ] } ], }, devServer: { historyApiFallback: true, contentBase: path.join(__dirname, 'dist'), compress: true, port: 9000 }, resolve: { extensions: ['*', '.js', '.jsx'], }, plugins: [ new HTMLWebpackPlugin({ template: './src/index.html', filename: './index.html' }) ], output: { path: path.resolve(__dirname, './dist'), filename: 'index.js', } }; # NPM Script Update "scripts": { # This is not working, webpack server. Any help really appreciated. "start": "webpack serve --mode development --open --hot", "build": "webpack --mode production" } # Folders for Source and Webpack Final Code mkdir -m777 src mkdir -m777 dist # Create index.js and index.html
index.js index.html
# Run in Dev Mode npm start # Production Mode npm run build
-
React team created sample code generator
npx create-react-app <app name> cd <app name> npm install npm start
Below are the basic building blocks of React.js
- ES6 and Above Features for React
- Mock Rest Api
- Fetch Api
- CSS
- Components
- JSX
- Props
- State
- Event Handler
- Lifecycle
- Context Api
- Forms
- Hooks
- Router
- Redux
- Material UI
- Storybook
- Jest
- MicroFront End