Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

usememo react

import React, { memo, useMemo, useState } from 'react'

const Heading = memo(({ style, title }) => {
  console.log('Rendered:', title)

  return <h1 style={style}>{title}</h1>
})

export default function App() {
  const [count, setCount] = useState(0)

  const normalStyle = {
    backgroundColor: 'teal',
    color: 'white',
  }

  const memoizedStyle = useMemo(() => {
    return {
      backgroundColor: 'red',
      color: 'white',
    }
  }, [])

  return (
    <>
      <button
        onClick={() => {
          setCount(count + 1)
        }}
      >
        Increment {count}
      </button>
      <Heading style={memoizedStyle} title="Memoized" />
      <Heading style={normalStyle} title="Normal" />
    </>
  )
}
Comment

useMemo

const memoizedResult = useMemo(() => {
  return expensiveFunction(propA, propB);
}, [propA, propB]);
Comment

react usememo

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

Returns a memoized value.

Pass a “create” function and an array of dependencies. useMemo will only recompute the memoized value when one of the dependencies has changed. This optimization helps to avoid expensive calculations on every render.

Remember that the function passed to useMemo runs during rendering. Don’t do anything there that you wouldn’t normally do while rendering. For example, side effects belong in useEffect, not useMemo.

If no array is provided, a new value will be computed on every render.

You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.
Comment

useMemo

/*
Pass a “create” function and an array of dependencies. 
useMemo will only recompute the memoized value when one 
of the dependencies has changed. This optimization helps 
to avoid expensive calculations on every render.
*/
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
Comment

useMemo example:

import React, { useEffect, useMemo } from "react"
import { massiveArray } from "./massiveArray"

export const filterAndMapMassiveArrayToNumber = number => {
  // There are much better ways and algorithms to do this sorting,
  // this is only for illustrating the purpose of useMemo.
  return massiveArray
    .map(item => ({
      ...item,
      value: item.value * myNumber,
    }))
    .filter(item => item.value > 10)
}

export default function UseMemoWithExpensiveFunctionExample({ myNumber = 1 }) {
  const finishedArray = useMemo(
    () => filterAndMapMassiveArrayToNumber(myNumber),
    [myNumber]
  )

  return (
    <ul>
      {finishedArray.map(item => (
        <li key={item.id}>{item.value}</li>
      ))}
    </ul>
  )
}
Comment

usememo

//when just a or b dependency change memoizedValie is work
// we are doing for when your component rendering but this memorizedValue to complexity have
// and you just want to render this complexity codes when a or b changes

const memoizedValue = useMemo(() => {
return computeExpensiveValue(a, b)

}, [a, b]);



Comment

PREVIOUS NEXT
Code Example
Javascript :: how to use datepicker apply to send a get request 
Javascript :: hot to set file views in nodejs 
Javascript :: axios display nested json console.log 
Javascript :: click on browser.find_element_by_xpath with href 
Javascript :: react native websocket useSession 
Javascript :: flask vue.js not working 
Javascript :: sendmediagroup telegram nodejs 
Javascript :: how to convert string to invert case in javascript 
Javascript :: discord.js v13 joinVoiceChannel 
Javascript :: react use media query 
Javascript :: what is functional programming 
Javascript :: js store function in variable 
Javascript :: autocomplete data selected validation jquery 
Javascript :: how to check if expo app is running on the web 
Javascript :: run a local instance of ElasticSearch on docker 
Javascript :: Make a program that filters a list of strings and returns a list with only your friends name in it.javascript 
Javascript :: window.history 
Javascript :: javaScript getDate() Method 
Javascript :: delate char betwen index js 
Javascript :: how to draw vertical dash line in react native 
Javascript :: javascript this inside function 
Javascript :: arguments in javascript 
Javascript :: Flutter list of JSONs to Objects 
Javascript :: js .length 
Javascript :: javscript randomly generate 89digit number 
Javascript :: Sequelize plain result 
Javascript :: delete character between index 
Javascript :: change array range value javascript 
Javascript :: jquery how to get element insde div 
Javascript :: axios.get Uncaught (in promise) TypeError: response.json is not a function 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =