Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react usememo

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

React useMemo Hook

//A poor performing function. The expensiveCalculation function runs on every render
import { useState } from "react";
import ReactDOM from "react-dom/client";

const App = () => {
  const [count, setCount] = useState(0);
  const [todos, setTodos] = useState([]);
  const calculation = expensiveCalculation(count);

  const increment = () => {
    setCount((c) => c + 1);
  };
  const addTodo = () => {
    setTodos((t) => [...t, "New Todo"]);
  };

  return (
    <div>
      <div>
        <h2>My Todos</h2>
        {todos.map((todo, index) => {
          return <p key={index}>{todo}</p>;
        })}
        <button onClick={addTodo}>Add Todo</button>
      </div>
      <hr />
      <div>
        Count: {count}
        <button onClick={increment}>+</button>
        <h2>Expensive Calculation</h2>
        {calculation}
      </div>
    </div>
  );
};

const expensiveCalculation = (num) => {
  console.log("Calculating...");
  for (let i = 0; i < 1000000000; i++) {
    num += 1;
  }
  return num;
};

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
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 :: react-load-animations 
Javascript :: context api 
Javascript :: jquery add attribute without value 
Javascript :: electron ipc from main to renderer 
Javascript :: charcodeat javascript 
Javascript :: discord.js if no arguments 
Javascript :: how to cast in javascript 
Javascript :: jQuery intellisence in VSCode 
Javascript :: How to pass data in Link of react-router-dom 
Javascript :: alert javascript react native 
Javascript :: how to append response header in node 
Javascript :: javascript substring 
Javascript :: js subtract days 
Javascript :: button disable in js 
Javascript :: search an array with regex javascript indexOf 
Javascript :: JavaScript POSITIVE_INFINITY 
Javascript :: how to change array element to integer in js 
Javascript :: how to add icons in angular 
Javascript :: how to copy array of objects in javascript 
Javascript :: Javascript using forEach loop to loop through an array 
Javascript :: react native stylesheet shortcut 
Javascript :: reactjs lifecycle class components 
Javascript :: else return 
Javascript :: ng2 validations angular using reactiveforms 
Javascript :: express post 
Javascript :: how to add the click event into two element in jquery 
Javascript :: infinite loop in javascript 
Javascript :: javscript rename property name 
Javascript :: filter js object array based on multiple parameters 
Javascript :: sort in array in javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =