KKitForma.

Language

EnglishEnglishTürkçeTurkishDeutschGermanGuide unavailable in this language · open toolsEspañolSpanishGuide unavailable in this language · open toolsFrançaisFrenchGuide unavailable in this language · open toolsPortuguêsPortugueseGuide unavailable in this language · open toolsItalianoItalianGuide unavailable in this language · open toolsNederlandsDutchGuide unavailable in this language · open toolsPolskiPolishGuide unavailable in this language · open toolsРусскийRussianGuide unavailable in this language · open toolsУкраїнськаUkrainianGuide unavailable in this language · open toolsSvenskaSwedishGuide unavailable in this language · open toolsNorskNorwegianGuide unavailable in this language · open toolsDanskDanishGuide unavailable in this language · open toolsSuomiFinnishGuide unavailable in this language · open toolsČeštinaCzechGuide unavailable in this language · open toolsRomânăRomanianGuide unavailable in this language · open toolsΕλληνικάGreekGuide unavailable in this language · open toolsالعربيةArabicGuide unavailable in this language · open toolsעבריתHebrewGuide unavailable in this language · open toolsفارسیPersianGuide unavailable in this language · open toolsاردوUrduGuide unavailable in this language · open toolsहिन्दीHindiGuide unavailable in this language · open toolsবাংলাBengaliGuide unavailable in this language · open toolsதமிழ்TamilGuide unavailable in this language · open toolsతెలుగుTeluguGuide unavailable in this language · open toolsमराठीMarathiGuide unavailable in this language · open toolsગુજરાતીGujaratiGuide unavailable in this language · open tools简体中文Chinese SimplifiedGuide unavailable in this language · open tools繁體中文Chinese TraditionalGuide unavailable in this language · open tools日本語JapaneseGuide unavailable in this language · open tools한국어KoreanGuide unavailable in this language · open toolsTiếng ViệtVietnameseGuide unavailable in this language · open toolsไทยThaiGuide unavailable in this language · open toolsBahasa IndonesiaIndonesianGuide unavailable in this language · open toolsBahasa MelayuMalayGuide unavailable in this language · open toolsFilipinoFilipinoGuide unavailable in this language · open toolsKiswahiliSwahiliGuide unavailable in this language · open toolsAfrikaansAfrikaansGuide unavailable in this language · open toolsMagyarHungarianGuide unavailable in this language · open toolsБългарскиBulgarianGuide unavailable in this language · open toolsHrvatskiCroatianGuide unavailable in this language · open toolsSrpskiSerbianGuide unavailable in this language · open toolsSlovenčinaSlovakGuide unavailable in this language · open toolsSlovenščinaSlovenianGuide unavailable in this language · open toolsLietuviųLithuanianGuide unavailable in this language · open toolsLatviešuLatvianGuide unavailable in this language · open toolsEestiEstonianGuide unavailable in this language · open toolsCatalàCatalanGuide unavailable in this language · open toolsEuskaraBasqueGuide unavailable in this language · open tools
← Questions & answers

KITFORMA · EDITORIAL ANSWER

Why does a memoized React callback submit an old record ID?

KitForma editorial guide. An empty useCallback dependency list retains the callback from the first render, including its original recordId.

React & Next.js →

Step-by-step answer

An empty useCallback dependency list retains the callback from the first render, including its original recordId. Add recordId and every other reactive value the callback reads. Put this component in App.jsx in a React application. Both buttons only update local display state; they send no request. The empty dependency list is deliberately wrong and should trigger the exhaustive-deps lint warning.
jsx
import { useCallback, useState } from "react";

export default function App() {
  const [recordId, setRecordId] = useState("A");
  const [savedId, setSavedId] = useState("none");

  // Deliberately broken: retains the first render's recordId.
  const saveStale = useCallback(() => setSavedId(recordId), []);
  // Correct: a changed recordId creates the current handler.
  const saveCurrent = useCallback(() => setSavedId(recordId), [recordId]);

  return <>
    <label>Record <select value={recordId}
      onChange={event => setRecordId(event.target.value)}>
      <option value="A">A</option><option value="B">B</option>
    </select></label>
    <button type="button" onClick={saveStale}>Save stale</button>
    <button type="button" onClick={saveCurrent}>Save current</button>
    <p role="status">Saved: {savedId}</p>
  </>;
}
Reproduce: start on A, select B, then press Save stale. The display says Saved: A although B is selected. Press Save current: it must say Saved: B. Select A and repeat; the corrected button must follow the current selection. In a real save handler, include reactive props such as an onSave callback too; setSavedId itself is stable. If a memoized child uses a custom comparator that ignores the callback prop, it may keep an old handler even after you fix the dependency list. Remove that comparator or compare function props as well. Do not suppress the dependency warning or add a timer to hide the stale value.

Sources and verification

Sources checked:

This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.

Discussion and community answers

Loading community…
KitForma

What would you like to do?