marmelab/react-admin · error · Error
You must provide a key to remove an item from the store
Error message
You must provide a key to remove an item from the store
What it means
useRemoveItemsFromStore returns a callback that removes items from react-admin's global store under a key or key prefix. If neither an argument nor a hook-time key prefix is provided, there is nothing to target, so the hook throws instead of silently doing nothing.
Source
Thrown at packages/ra-core/src/store/useRemoveItemsFromStore.ts:29
* const ResetDatagridPrefs = () {
* const removeItems = useRemoveItemsFromStore();
*
* const handleClick = () => {
* removeItems('datagrid.prefs');
* };
*
* return <Button onClick={hancleClick}>Reset datagrid preferences</Button>;
* }
*/
export const useRemoveItemsFromStore = (hookTimeKeyPrefix?: string | null) => {
const { removeItems } = useStoreContext();
return useCallback(
(keyPrefix?: string) => {
if (
typeof keyPrefix === 'undefined' &&
typeof hookTimeKeyPrefix === 'undefined'
) {
throw new Error(
'You must provide a key to remove an item from the store'
);
}
// @ts-ignore
return removeItems(keyPrefix ?? hookTimeKeyPrefix);
},
[removeItems, hookTimeKeyPrefix]
);
};
View on GitHub (pinned to 051f511bb0)
Solutions
- Pass the key (or key prefix) when calling the returned callback: removeItems('my.key').
- Provide the key at hook level: useRemoveItemsFromStore('my.key') so call-time argument is optional.
- If the key is dynamic, guard the call: if (key) removeItems(key).
Example fix
// before
const removeItems = useRemoveItemsFromStore();
removeItems(); // throws
// after
const removeItems = useRemoveItemsFromStore('my.key');
removeItems(); // or removeItems('my.key') Defensive patterns
Strategy: validation
Validate before calling
if (typeof key !== 'string' || key.length === 0) {
throw new Error('removeItemsFromStore requires a key');
}
removeItems(key); Type guard
const hasKey = (k: unknown): k is string => typeof k === 'string' && k.length > 0;
Try / catch
try {
removeItems(key);
} catch (e) {
console.error('Failed to remove store item', { key, e });
} Prevention
- Always pass the storage key explicitly to the remove callback.
- Bind the key at hook level when it is static: useRemoveItemsFromStore('my.key').
- Add a lint rule or team convention that store keys are constants imported from one module.
When it happens
Trigger: Calling the returned removeItems function with no argument (keyPrefix === undefined) while the hook was also created without a hookTimeKeyPrefix, e.g. const removeItems = useRemoveItemsFromStore(); removeItems();
Common situations: Developers wiring a 'clear stored preference' button and forgetting to pass the storage key, or refactoring useStore usage where the key was previously fixed at hook level and is now expected at call time.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- useCanAccess must be used inside a <Resource> component or p
- useCreateController requires a non-empty resource prop or co
- useEditController requires a non-empty resource prop or cont
- useArrayInput must be used inside an ArrayInputContextProvid
- useSimpleFormIterator must be used inside a SimpleFormIterat
AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30).
Data as JSON: /api/errors/a40ef91bcab676f4.
Report an issue: GitHub.