Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
AI & KI NachrichtenIs the AI industry really ready to slow down?(20.09.2026 um 20:56 Uhr)
Sichere ProgrammierungThe audio bugs nobody warns you about when you build a mobile looper(20.09.2026 um 20:18 Uhr)
Sichere ProgrammierungA Successful Response Can Still Belong to the Wrong Screen(20.09.2026 um 20:23 Uhr)
Sichere ProgrammierungGzip 1.15 fixes a wrong-file deletion race(20.09.2026 um 20:32 Uhr)
Sichere ProgrammierungWhen SQL Has Nothing to Say: Handling NULLs(20.09.2026 um 20:35 Uhr)
Sichere ProgrammierungWeb Programming in C++ with WFC(20.09.2026 um 20:37 Uhr)
AI & KI NachrichtenIs the AI industry really ready to slow down?(20.09.2026 um 20:56 Uhr)
Sichere ProgrammierungThe audio bugs nobody warns you about when you build a mobile looper(20.09.2026 um 20:18 Uhr)
Sichere ProgrammierungA Successful Response Can Still Belong to the Wrong Screen(20.09.2026 um 20:23 Uhr)
Sichere ProgrammierungGzip 1.15 fixes a wrong-file deletion race(20.09.2026 um 20:32 Uhr)
Sichere ProgrammierungWhen SQL Has Nothing to Say: Handling NULLs(20.09.2026 um 20:35 Uhr)
Sichere ProgrammierungWeb Programming in C++ with WFC(20.09.2026 um 20:37 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

useMemo

Reagiere als Erste:r — dein Feedback zählt!

In react, when variables are declared inside the component, they get re-created on every render

Normally, this wouldn't be a problem. But,
what if the function is expensive?
What if it takes a longer time to execute?
Imagine if there were multiple state variables in the component, each is not dependent on the other. Each state variable update would cause a re-render and execute an expensive function.

This would cause unnecessary delays. This would affect the performance of application and could lead to a terrible user experience.

Take the following example:

function calculate() {
  let result = 0;
  for (let i = 0; i < 1000000000; i++) {
    result += i;
  }
  return result;
}

function App1() {
  const [count, setCount] = useState(0);

  const value = calculate();

  return (
    <div className="App">
      <button onClick={() => setCount(count + 1)}>Increment Count</button>
      <p>Count: {count}</p>
    </div>
  );
}

In this scenario, useMemo entry is needed.

useMemo is a hook used for optimizing expensive calculations by memoizing the result

useMemo memoizes the return value of expensive calculations between renders.

Memoization is a technique that speeds up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again

useMemo can help you tackle this issue.
The useMemo hook should be declared at the top level of your component. It takes the following arguments:

const value = useMemo(expensiveFunction, [...dependencyArray])

Expensive function
If you declare the expensive function outside the component, you should pass the function reference (not invoke it directly) to useMemo. Alternatively, you can define an inline arrow function within useMemo.

Example 1: Function declared outside and passed by reference

// Expensive function declared outside the component
const expensiveFunction = (num) => {
  console.log("Expensive calculation");
  return num * 10;
};

const MyComponent = ({ number }) => {
  // Use useMemo to memoize the result
  const result = useMemo(() => expensiveFunction(number), [number]);

  return <div>Result: {result}</div>;
};

Here, expensiveFunction is declared outside the component, and you pass it by reference to useMemo. The computation only re-runs when number changes.

Example 2: Using an arrow function directly

const MyComponent = ({ number }) => {
  // Use useMemo with an inline arrow function for the expensive calculation
  const result = useMemo(() => {
    console.log("Expensive calculation");
    return number * 10;
  }, [number]);

  return <div>Result: {result}</div>;
};

In this case, the expensive calculation is done within the arrow function passed directly into useMemo. The memoized result is recalculated only when number changes.

Dependency Array
contains list of dependencies for the hook. The expensive function will be called only when one of these dependencies is updated. You can pass state variables or props that are dependent on this calculation. Any other state updates will not trigger the function.

When to use useMemo:
When you have a state dependent on an expensive calculation, but you don't want to run the calculation on every render.

When you declare an array or object inside a component, its reference changes on every render, even though the value remains the same. Wrapping the values inside useMemo maintains referential equality and prevents unnecessary re-renders. This is essential when there's a useEffect dependent on the array or object.

When you are rendering lists using Array.map that do not need to change unless a certain state value changes.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten useMemo

Thematisch verwandte Begriffe: useMemo · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93956 | A flaw has been found in olivier-ls PHP-FTS up to 1.1.2. Affected by thi…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick