GithubHelp home page GithubHelp logo

wichaisw / practice_react-lazy-hooks-tailwind Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 202 KB

a repository for practice using various hooks & others in React: useRef, code splitting with React.lazy, reusable Tailwind component.

JavaScript 77.97% HTML 16.27% CSS 5.77%
react react-lazy hooks-api tailwind-css

practice_react-lazy-hooks-tailwind's Introduction

React.memo()

  • Won't render if 'props' isn't changed.

useRef(): examples in Page1

  • Persist value between each render.
  • don't use ref to change value of states, do that with useState.
  • don't use ref to appendChild, let jsx handle it.
  • use cases are:
  1. Show how many times has a component been rendered
  2. Reference an element inside HTML.
  3. Get previous value of state vis useEffect

forwardingRef

  • Parent attaches its ref to child. parent will be able to access child DOM element with the ref.current
  • This also helps the child to read and modify the element from any location where it is used.

useMemo()

  • Returns a memoized value.
  • use case: if there is an expensive function, we might want to run it only when any arguments is changed (which change result in the process), useMemo can be used to remember the returned value and make the function run again only when arguments are changed.
  • Function passed to useMemo runs during rendering.
  • Side effects belong in useEffect, not useMemo.

const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);

useCallback(): examples in Page2

  • Returns a memoized callback.
  • Solves referential equality problem
  • use case: if a function is passed as prop, and that function is referenced by useEffect, it will run every times its parent re-render. useCallback can prevent that.
  • UseCallback(fn, deps) is equivalent to useMemo(() => fn, deps).

const memoizedCallback = useCallback( () => { doSomething(a, b); }, [a, b], );

useMemo vs useCallback

  • useMemo take a function and return the value of that function.
  • useCallback take a function and return that function

React.lazy(): an example in App

  • Must always have tag as parent, which have fallBack component as its props.
  • Just doing code splitting by import the lazy component when need to be re-rendered.

practice_react-lazy-hooks-tailwind's People

Contributors

wichaisw avatar

Stargazers

 avatar

Watchers

 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.