{"record":{"id":"550f15729c225bb7","repo":"marmelab/react-admin","slug":"you-must-provide-a-key-to-remove-an-item-from-the","errorCode":null,"errorMessage":"You must provide a key to remove an item from the store","messagePattern":"You must provide a key to remove an item from the store","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/ra-core/src/store/useRemoveFromStore.ts","lineNumber":29,"sourceCode":" * const ResetDatagridPrefs = () {\n *    const removeItem = useRemoveFromStore();\n *\n *    const handleClick = () => {\n *        removeItem('datagrid.prefs');\n *    };\n *\n *    return <Button onClick={hancleClick}>Reset datagrid preferences</Button>;\n * }\n */\nexport const useRemoveFromStore = (hookTimeKey?: string) => {\n    const { removeItem } = useStoreContext();\n    return useCallback(\n        (key?: string) => {\n            if (\n                typeof key === 'undefined' &&\n                typeof hookTimeKey === 'undefined'\n            ) {\n                throw new Error(\n                    'You must provide a key to remove an item from the store'\n                );\n            }\n            // @ts-ignore\n            return removeItem(key ?? hookTimeKey);\n        },\n        [removeItem, hookTimeKey]\n    );\n};\n","sourceCodeStart":11,"sourceCodeEnd":39,"githubUrl":"https://github.com/marmelab/react-admin/blob/051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0/packages/ra-core/src/store/useRemoveFromStore.ts#L11-L39","documentation":"useRemoveFromStore returns a function to delete a key from the react-admin store. The key can be supplied at hook time (useRemoveFromStore(key)) or at call time (removeItem('key')); if both are undefined there is nothing to remove, so react-admin throws instead of silently no-oping.","triggerScenarios":"Calling useRemoveFromStore() with no hook-time key and then invoking the returned callback without arguments; calling removeItem(undefined) from a variable that is undefined due to unloaded data or a missing prop.","commonSituations":"Custom store-cleanup buttons where the key lives in state that is still undefined; refactors that moved the key from the callback argument to the hook argument (or vice versa) leaving neither populated; key built from a record field that doesn't exist yet.","solutions":["Pass the key explicitly: const removeItem = useRemoveFromStore(); removeItem('preferences.posts');","Or provide the hook-time key: const removeItem = useRemoveFromStore('foo'); then removeItem().","Guard before calling: if (key) removeItem(key) — only invoke once the key is known."],"exampleFix":"// before\nconst removeItem = useRemoveFromStore();\nremoveItem(); // throws\n\n// after\nconst removeItem = useRemoveFromStore();\nremoveItem('my.key');\n// or hook-time key\nconst removeItem = useRemoveFromStore('my.key');\nremoveItem();","handlingStrategy":"validation","validationCode":"const removeItem = useRemoveFromStore();\nif (key == null) {\n    console.warn('No key provided to removeItem');\n    return;\n}\nremoveItem(key);","typeGuard":"const hasStoreKey = (k: string | undefined): k is string => typeof k === 'string' && k.length > 0;","tryCatchPattern":"try {\n    removeItem(key);\n} catch (e) {\n    console.error('removeFromStore requires a key', e);\n}","preventionTips":["Decide on one convention: pass the key to useRemoveFromStore OR to the callback, never rely on both being optional.","Check that keys derived from record fields exist before calling removeItem.","Keep keys as constants to avoid undefined propagation from dynamic strings."],"tags":["store","validation","argument-error"],"backgroundTag":"missing-required-argument","analyzedSha":"051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0","analyzedAt":"2026-08-30T02:28:14.926Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}