Giter VIP home page Giter VIP logo

maxsection12's Introduction

maxSection12

  • Every state Changes, Prop changes, context changes, would result in this compoenent fucntion, being executed again
  • for the reactDom, only the affected tag will affected and change
  • In react, the child component will affected if there is change in parent component too.

React.memo Prevent unnecessary render

  • React.memo tells React that for the component, React should look at the props that the component gets
  • and check the new value for all those props
  • and compare it to the previous value those props got,
  • and only if the value of a prop changed, the component should re-executed.

Why we dont use it in all component

  • React needs to do 2 thungs
  1. it needs to store the previous prop valus
  2. do comparison
  • memo is good as you can avoid unnecessart re-render cycles
  • Limitations :
    • it not works on reference objects like object, array and functions

useCallback

  • alternative of React.memo for reference object
  • is a hook that allows us to store a function across component executions
  • it allows us to tell React that we wanna save a function and the function should not be recreated

Sometimes, you might think, why do we need dependency array on useCallback, as my function always the same logic accorss rerender cycles

  • In Javascript, functions are closure which means when a function is defined. Javascript basically locks in all the variable that we are using in there.
  • As the component is rerender and reexecuted, the function inside the useCallback is not recreated under all the circumstances
  • all the stored Values is used not a new one when component is rerender
  • so we would need a array dependecies

Important to know Closure and Primitive Reference Value

Dive in useState *You might wonder why useState go back to it initial

  • React got special handle it and same as useReducer

useMemo

  • React.memo use for whole component, while we can use useMemo on specific thing inside a compoenent
  • we can use useMemo when we do calculation in a component

For Example ** const sortedList = useMemo(()=>{ return props.items.sort((a, b)=>a-b); },[props.items]); ** At Parent Component ** const listItems = useMemo(()=> [5,3,1,10,9],[])

maxsection12's People

Watchers

Kai Xuan Tan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.