Giter Site home page Giter Site logo

rocketseat-reactjs-2021-desafio08's Introduction

README

Utilizar as técnicas de otimização (Memo, UseMemo e UseCallback) no projeto

Ordem de renderização no React

  • passo 1: criar uma nova versão do componente (vdom)
  • passo 2: comprarar com a versão anterior (vdom)
  • passo 3: atualizar as alterações (dom)

Memo

Impede a criação de uma nova versão do componente (passo 1) se o props for o mesmo (shallow comparation)

Onde usar:

  • Pure functions component
  • Renders too often
  • Re-renders with same props
  • Medium to big sized components
import { memo } from 'react';

function AppComponent() {}

export const App = memo(AppComponent, (previousProps, nextProps) => {
    return Object.is(previousProps, nextProps) // deep comparataion
})

UseMemo

Onde usar:

  • Cálculos pesados
  • Igualdade referêncial (quando repassa algum valor para o componente filho)
import { useMemo } from 'react';

export function App({products}) {
    const bestOfferByCategory = useMemo((products) => {
        return {} // heavy calculations
    }, [products])
}

UseMemo

Onde usar:

  • Igualdade referêncial (quando repassa alguma função para o componente filho)
import { useCallback, useState } from 'react';

export function App({products}) {
    const [favProducts, setFavProducts] = useState([])
    const addFavProduct = useCallback(async (productId) => {
        setFavProducts(favProducts => [...favProducts, productId])
    }, [])
}

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.