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

  • 1.
  • 4.
    Library required • 1.React JS library • 2. ReactDOM library
  • 5.
    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 {}
  • 6.
    [ { id :1, name : “John”, age : 12 }, { id : 1, name : “John”, age : 12 }, ]
  • 7.
    Components • Collection ofelements • Increase readability • Reduce complexity • Reuse the code
  • 8.
    Components • There aretwo ways to create Components • Functional Components • Class Components
  • 9.
    Assignment • Create TwoPages • Functional Components – Inline styles • Class Components – External styles
  • 10.
    Events • onclick • onkeyup •onkeydown • onmouseover • onmouseout • onchange • onload
  • 11.
    Rules for FunctionalComponents • Should be Pure Functions. • It should atleast take one parameter • It should return React Elements • It should not modify the parameters data
  • 12.
    CRUD • C –Create - state • R – Read – props • U – Update - state • D – Delete – state
  • 13.
    state • States aremaintained or present only in Class components
  • 14.
    Pass data fromParent to Child • Use props to pass data from parent to child
  • 15.
    Pass data fromchild 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
  • 16.
    Libraries to workwith react • React JS library • React DOM library • Babel library • Live Server – webpack-dev-server • Project Structure • Webpack - bundler
  • 17.
    Npm – nodepackage manager • https://nodejs.org/en/download/ • Check the version • node –v • npm –v
  • 18.
    Command to createreact project • npx create-react-app app-name • npx create-react-app first-react • cd first-react • code . • Ctrl ` - open the terminal • npm start
  • 19.
  • 20.
    LifeCycle • Start Phase1  Phase2  Phase3  End • Once  lot of phases Once • Birth  Child  Adult  Married  Old  Death
  • 21.
    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
  • 22.
    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
  • 23.
    Class Component LifeCycle •UnMounting – Whenever component is unmounted from the real dom • componentWillUnmount() - clean up process , clear localStorage
  • 24.
    Router – toachieve SPA • npm install react-router-dom --save
  • 26.
    Javascript • AJAX call •Fetch api • Axios – HTTP Client which based on Promises.
  • 27.
    CRUD • Create -POST • Read - GET • Update - PUT • Delete - DELETE
  • 28.
  • 31.
    React Hooks • ReactHooks – normal javascript functions
  • 33.
    • Primitive valueswill be compared. • Reference address wil be compared. • Object or array. const person ={ name : “soni”, age : 12 } const person ={ name : “soni”, age : 12 }
  • 34.
    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)
  • 35.
    React.memo • To makefunctional component pure component
  • 36.
    React Hooks • Javascriptfunctions. • React hooks should be called only in functional components. • It should be defined in top level.
  • 37.
    State in classcomponent 1. Initialize the state 2. Current state data (this.state) 3. Modify the state ( this.setState({ }) )
  • 38.
    State in functionalcomponent • const array = useState(initialState) • array[0] = current state data • array[1] = function to modify the state
  • 39.
    Best Practice toupdate 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)
  • 40.
    useEffect() • It isa alternative all three methods • componentDidMount • componentDidUpdate • componentWillUnmount
  • 41.
    useEffect(callback function) • useEffect(()=>{ //component is mounted // state or props getting changed return () =>{ // component is unmounted } })
  • 42.
    useEffect first syntax •useEffect(()=>{ // logic for mounting }, [] ) ===== componentDidMount
  • 43.
    useEffect second syntax •useEffect(()=>{ // logic executes whenever there is change in count variable value }, [ count ])
  • 44.
    useEffect third syntax •useEffect(()=>{ return () =>{ // logic to be executed on component unmount } }, [])
  • 45.
  • 46.
    States while makinga request to server(REST call or AJAX call) • Loading • Error • Response data - Success
  • 47.
  • 48.
    useReducer(reducer,initialState) new state =reducer(state,action) const [ newState, dispatch ] = useReducer(reducer,initialState)
  • 49.
    useState vs useReducer Primitive(number,Boolean,string ) – useState Object , Array – useReducer Global – useReducer Local – useState Business logic – useReducer No business logic - useState