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

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.

Source

Thrown at packages/ra-core/src/store/useRemoveFromStore.ts:29

 * const ResetDatagridPrefs = () {
 *    const removeItem = useRemoveFromStore();
 *
 *    const handleClick = () => {
 *        removeItem('datagrid.prefs');
 *    };
 *
 *    return <Button onClick={hancleClick}>Reset datagrid preferences</Button>;
 * }
 */
export const useRemoveFromStore = (hookTimeKey?: string) => {
    const { removeItem } = useStoreContext();
    return useCallback(
        (key?: string) => {
            if (
                typeof key === 'undefined' &&
                typeof hookTimeKey === 'undefined'
            ) {
                throw new Error(
                    'You must provide a key to remove an item from the store'
                );
            }
            // @ts-ignore
            return removeItem(key ?? hookTimeKey);
        },
        [removeItem, hookTimeKey]
    );
};

View on GitHub (pinned to 051f511bb0)

Solutions

  1. Pass the key explicitly: const removeItem = useRemoveFromStore(); removeItem('preferences.posts');
  2. Or provide the hook-time key: const removeItem = useRemoveFromStore('foo'); then removeItem().
  3. Guard before calling: if (key) removeItem(key) — only invoke once the key is known.

Example fix

// before
const removeItem = useRemoveFromStore();
removeItem(); // throws

// after
const removeItem = useRemoveFromStore();
removeItem('my.key');
// or hook-time key
const removeItem = useRemoveFromStore('my.key');
removeItem();
Defensive patterns

Strategy: validation

Validate before calling

const removeItem = useRemoveFromStore();
if (key == null) {
    console.warn('No key provided to removeItem');
    return;
}
removeItem(key);

Type guard

const hasStoreKey = (k: string | undefined): k is string => typeof k === 'string' && k.length > 0;

Try / catch

try {
    removeItem(key);
} catch (e) {
    console.error('removeFromStore requires a key', e);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30). Data as JSON: /api/errors/550f15729c225bb7. Report an issue: GitHub.