React.js
GitHub Repo: https://github.com/YingzeHou/portfolio.git
Installation
Download Node.js
Run VScode as admin role in the target directory
npm install -g create-react-app
npx create-react-app <your-app-name>.
cd <your-app-name>
Delete unneccesary files like icon, css, etc. (Keep App.js & index.js)
npm start
to start the development server
StartUp
Create components and sub-components folders under src (Use .jsx and .scss files)
yarn add node-sass
to enable use of scss fileDelete pre-exist package-lock.json and run
npm install
to download all required dependencies on local machine
Syntax
Control scroll type and scroll alignment
Built-in boolean function to change state
Animation in Scss (use @keyframes to define the action name and behavior)
Deployment
In the project folder, use npm
install gh-pages --save-dev
/yarn add gh-pages --save-dev
to apply github pages dependenciesGo to package.json and add "homepage" at the very beginning and "predeploy", "deploy" at npm script:
3. Create a github repo and push local project to remote repo
4. Run npm run deploy
to deploy the website, thus website is available at homepage url
History & Overview
WHY: JS library for building user interfaces
DOM for single-page applications (SPAs) can be huge
Large number of updates on DOM elements
Virtual DOM
Virtual representation of the user-facing elements that are kept in memeory and synced with the real DOM when DOM elements are updated.
Reconciliation
The process to diffing and syncing the virtual and real DOM to render changes for the user.
Declaritive
Express logic of a computation without describing its flow: What we want the outcome to be
ReactDOM library takes care of reconciliation and updating user-facing content under the hood
Building Block
Elements: Light, stateless, immutable, virtual representation of a DOM element
Generate new element to update the page
Components: A function or class that accepts an input and returns a React element
Works like JS functions; Accept props; Each component is encapsulated (one component per file) and can operate independently, affording modularity.
render()
Returns a React element to be displayed on the page. WHAT to see on screen-->into render()
ReactDOM.render(): Mounts the declared element as a child to the specified container in the DOM.
Component.render(): Creates the virtual DOM representation of the contents of the React component. Then, we call ReactDOM.render() to mount it to the real DOM
props:
Properties, the arbitrary input provided into React component that utilize them to render content.
Component never modify props; Read-only & Immutable;
states
Fully private and controlled by the component
Keep track of changes in data.
JSX
Syntax extension to JavaScript, that adds XML syntax to JavaScript. JSX declarations produce React elements.
Makes React programming extremely effective
Naming Convention
camelCase involves writing phrases such that each word begins capitalized with no spaces. (ReactDOM & JSX)
hyphen-case involves writing phrases in lower case and using a hyphen as a seperator.
PascalCase capitalizes all words with no spaces (React Components)
Component Library
Abstract away the low-level CSS implementation of user-facing elements.
Bootstrap: (1) CDN=JS (2) Bootstrap dependency (3) React Bootstrap package
Foundation
Semantic UI
Pure
UIkit
Component Development & Reuse
Mock-up design: Sketch out the design
Break the UI into component hierarchy: Structure of the component->tree layout
Build static version: HTML component layout: div->child DOM structure
Identify the minimal set of mutable state: Check elements to see if changeable
Identify where your state should live: (1) App track state; (2) TACard keep track; (3) TAContactButton keep track: Scope of the state, #3 most aligned with React way (Bestly, state need to be as close/low level as possible (component))
Add inverse data flow: Parent->Child: props; Child->Parent: callback returns
Fragment
React constructs that can group child components without adding extra nodes to the DOM
<React.Fragment> can be automatically removed when groupping, simplify DOM structure
Controlled vs Uncontrolled Components
States of controlled components are managed by React. User input element (input, textsarea, select...) uncontrolled. Use refs to give React access to DOM elements
this.bind()
this.<functionName>.bind(this), clarifies that the scope of the function that is passed to children component is within the parent component.
Calling(top): function()-->Call directly
Passing(bottom): function-->Pass as pointer
Hook
Hooks are functions that let you "hook into" React state and lifycycle features from function components. Hooks don't work inside classes — they let you use React without classes.
useState
useState is a Hook that lets you add React state to function components.
useEffect
useEffect lets you perform side effects in function components.
If called everytime there's an update: leave [state] blank
If called only once when mount: empty array [] at [state]
If called only when certain state changes: put the state into [state]
return() function is same as unmount cleanup.
Hooks can only be used in React function, not class and not regular JavaScript functions
Only call Hooks at top level, not inside loops, conditions, or nested function
Advanced Async Updating
async funtion()
denotes that the function() will work async
await expression
enables the program to wait for this expression to be resolved
Optimizing Performance
Do not deal with unneccesary re-render
Performance Tool
Eliminate time wasted
shouldComponentUpdate()
For components that implement shouldComponentUpdate(), React will only render if it returns true
Shallow Comparison: Strict equality, reference same
Deep Comparison: Property are compared: Lodash isEqual()
Use PureComponent: implements SCU automatically
Do not mutate data when using PureComponent: use same object to update instead of assigning the object to a new reference and update the reference.
API for advanced interation
Interaction Libraries
Component Libraries
Managing Data
Last updated