React JS Workings
Library required β€’ 1. React JS library β€’ 2. ReactDOM library
document.createElement(β€œh1”) React.createElement(β€œh1”,null,”Content”) const element = <h1 key=value ></h1> Value can be only two things 1. String 2. Anything other than string it should be enclosed with {}
[ { id : 1, name : β€œJohn”, age : 12 }, { id : 1, name : β€œJohn”, age : 12 }, ]
Components β€’ Collection of elements β€’ Increase readability β€’ Reduce complexity β€’ Reuse the code
Components β€’ There are two ways to create Components β€’ Functional Components β€’ Class Components
Assignment β€’ Create Two Pages β€’ Functional Components – Inline styles β€’ Class Components – External styles
Events β€’ onclick β€’ onkeyup β€’ onkeydown β€’ onmouseover β€’ onmouseout β€’ onchange β€’ onload
Rules for Functional Components β€’ Should be Pure Functions. β€’ It should atleast take one parameter β€’ It should return React Elements β€’ It should not modify the parameters data
CRUD β€’ C – Create - state β€’ R – Read – props β€’ U – Update - state β€’ D – Delete – state
state β€’ States are maintained or present only in Class components
Pass data from Parent to Child β€’ Use props to pass data from parent to child
Pass data from child to parent β€’ Step 1 – Create a method in parent component β€’ Step 2 – Pass that method as a props to child component β€’ Step 3 – Get that method from child component and call that method on action from the child component β€’ Step 4 – If you want to pass the data pass it as a parameter
Libraries to work with react β€’ React JS library β€’ React DOM library β€’ Babel library β€’ Live Server – webpack-dev-server β€’ Project Structure β€’ Webpack - bundler
Npm – node package manager β€’ https://nodejs.org/en/download/ β€’ Check the version β€’ node –v β€’ npm –v
Command to create react project β€’ npx create-react-app app-name β€’ npx create-react-app first-react β€’ cd first-react β€’ code . β€’ Ctrl ` - open the terminal β€’ npm start
β€’ App β€’ PersonList β€’ Person
LifeCycle β€’ Start οƒ  Phase1 οƒ  Phase2 οƒ  Phase3 οƒ  End β€’ Once οƒ  lot of phases Once β€’ Birth οƒ  Child οƒ  Adult οƒ  Married οƒ  Old οƒ  Death
Class Component LifeCycle β€’ Mounting Phase β€’ Constructor() – initialize state, no business logic , bind the methods, no side effects β€’ getDerivedStateFromProps() – return null or return new state, no side effects. β€’ render() – Presentation logic, no side effects. β€’ componentDidMount() – make side effects
Class Component LifeCycle β€’ Updating Phase β€’ getDerivedStateFromProps() – no side effects β€’ shouldComponentUpdate() – return true or false β€’ render() – Presentation logic β€’ getSnapshotBeforeUpdate() – return null or somedata β€’ shouldComponentUpdate() – make side effects
Class Component LifeCycle β€’ UnMounting – Whenever component is unmounted from the real dom β€’ componentWillUnmount() - clean up process , clear localStorage
Router – to achieve SPA β€’ npm install react-router-dom --save
Javascript β€’ AJAX call β€’ Fetch api β€’ Axios – HTTP Client which based on Promises.
CRUD β€’ Create - POST β€’ Read - GET β€’ Update - PUT β€’ Delete - DELETE
β€’ [ { email:””, password:””, phoneno:””, name:”” },{ } ]
React Hooks β€’ React Hooks – normal javascript functions
β€’ Primitive values will be compared. β€’ Reference address wil be compared. β€’ Object or array. const person ={ name : β€œsoni”, age : 12 } const person ={ name : β€œsoni”, age : 12 }
Component vs PureComponent β€’ Component – always render method will be called when the state or props change or setState method is executed (shouldComponent() – compare old props and new props and old state and new state) β€’ PureComponent – if the props or state is getting changed then only render method will be called otherwise render method will not be called. β€’ (Does Shallow Comparison – Use only for primitives) β€’ (Does not do deep comparison – Don’t use it for reference)
React.memo β€’ To make functional component pure component
React Hooks β€’ Javascript functions. β€’ React hooks should be called only in functional components. β€’ It should be defined in top level.
State in class component 1. Initialize the state 2. Current state data (this.state) 3. Modify the state ( this.setState({ }) )
State in functional component β€’ const array = useState(initialState) β€’ array[0] = current state data β€’ array[1] = function to modify the state
Best Practice to update state in Functional Component β€’ Take a copy of the state β€’ Update or modify or delete or add to the copy β€’ Set the state using the copy Example – const [numbers,setNumbers] = useState([1,2,3,4,5]) const n = […numbers] const result = n.filter(num=>{return num>2}) setNumbers(result)
useEffect() β€’ It is a alternative all three methods β€’ componentDidMount β€’ componentDidUpdate β€’ componentWillUnmount
useEffect(callback function) β€’ useEffect(()=>{ // component is mounted // state or props getting changed return () =>{ // component is unmounted } })
useEffect first syntax β€’ useEffect(()=>{ // logic for mounting }, [] ) ===== componentDidMount
useEffect second syntax β€’ useEffect(()=>{ // logic executes whenever there is change in count variable value }, [ count ])
useEffect third syntax β€’ useEffect(()=>{ return () =>{ // logic to be executed on component unmount } }, [])
Dummy Server β€’ https://jsonplaceholder.typicode.com/
States while making a request to server(REST call or AJAX call) β€’ Loading β€’ Error β€’ Response data - Success
useCallback β€’ useCallback(()=>{ },[age]) useCallback(()=>{ },[salary])
useReducer(reducer,initialState) new state = reducer(state,action) const [ newState, dispatch ] = useReducer(reducer,initialState)
useState vs useReducer Primitive (number,Boolean,string ) – useState Object , Array – useReducer Global – useReducer Local – useState Business logic – useReducer No business logic - useState

React JS Workings Exercises Extra Classes