Intro to React
JSX- JavaScript XMLJSX-JS(Webpack + babel)
Why className?
classis a reserved keyword inJSfor,class➡️ reserved keyword ➡️htmlFor,className
Purpose of index.js
Bootstraps (start) the react
<noscript></noscript>
Msg is displayed when - JS is disabled in the browser
Template syntax ➡️ {}
- Supports expression
- Assignable statement is an expression
- Only needed in
return
Why alt?
- Image placeholder
- Visually impaired people - Screen reader
- SEO - Ranking
React.StrictMode - Error checking
Displays Error in console
function to be considered as a component
- function should start with a capital letter
- It should have at least one
JSXelement
props ➡️ arguments
props - properties of the components
React fragment <></> or div
- Avoiding extra elements in the
DOM - Styling is easier for parent
Handy shortcuts
ctrl + `show / hide terminalctrl + ,settings | format on saveonctrl + cstop react server in terminalwin + .emojiscrtl + click- for easy navigation
Hooks
- React listen to changes made by hooks
- Hooks - function
- It starts with
use useStateconst [state, setState] = useState(InitialValue)- state - state of mind | state of the art technology | current
- In App state ➡️ current data
setStateupdates the value ofstatesetStatereact listens and updatesstate& viewimport { useState } from "react";- Flow: User clicks ➡️
onClicktrigger ➡️setLike➡️ React updates view
Toggle element
- Conditional rendering
- Conditional styling
Virtual DOM
- Copy of the Real Dom
- Large
{key: value} - Updating it does not cost performance
- To access VDOM you could use hooks (eg.
setState) - Once the update is done on the virtual DOM, then the Real dom is updated
- Comparison - Reconciliation - VDOM & Real Dom
- Key helps is quickly compare the changes
- Don't use
document.*in React - Loses performance
Color game
- Typing
eventlistner-onChange - Flow: User Types ➡️
onChangetrigger ➡️setColor➡️ React updates view - Add Color ➡️
setColorList➡️ Copy existingcolorList& add new color to it
imports & exports
- Types
- named imports & exports - preferred
- Multiple exports
- default imports & exports
- Only export per file
- Always put export at end of the file - good practice
crtl + click- for easy navigation
Material installation
npm install @mui/material @emotion/react @emotion/stylednpm install @mui/icons-material
_4<link_4 rel="stylesheet"_4 href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"_4/>
_4<link_4 rel="stylesheet"_4 href="https://fonts.googleapis.com/icon?family=Material+Icons"_4/>
React router
npm i react-router-dom
Tic Tac Toe
- Alternate | X -> O -> X | - ✅
- Board tracking - ✅
- Winning condition - ✅
- When player play check in winning condition appears on the board - ✅
- Announce winner - ✅
- Stop the game - ✅
Routing
Linkchanges URL without refreshRoutematchespath(URL) ->Component
Lifting the state up
- Lifting the state from child to parent
- Moving the declared state from child to parent
- Usage Sharing data between sibling components
sx
- Converts inline style
->internal style - Customize material components
- Available only for material components
Unmounting phase
Conditional renderingRouting
Data re-fetch
- Route change cause recreation of component
- Hence component is mounted, thus
useEffectis called - Advantage: Keeps data latest
Create, Read, Update & Delete (CRUD) seen in day-to-day life apps
renderProps
- Rendering props as
JSX - Advantage:
JSXhas access toparentFnvariables
Race Condition
GETmovies gets called, beforeDELETEcompletes- Will get OLD data
.thenorawait- breaks race condition
Post method - fetch
- Method -
POST - Data (newMovie) - body &
JSON - Header -
JSON
JSON vs JS object
JSONformat - string datatype- JS object - object datatype
- JS object Advantage
dot&boxsyntaxfunction
JSONformat - cannot have afunction- Why
JSON?- Universal format -
JS-python-GO
@ragavkumarv
swipe to next ➡️
