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
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
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
β’ 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)
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
useState vs useReducer Primitive(number,Boolean,string ) β useState Object , Array β useReducer Global β useReducer Local β useState Business logic β useReducer No business logic - useState