Back to React Admin

The useRecordFromLocation Hook

docs/useRecordFromLocation.md

5.14.62.1 KB
Original Source

useRecordFromLocation

Return a record that was passed through either the location query or the location state.

You may use it to know whether the form values of the current create or edit view have been overridden from the location as supported by the Create and Edit components.

Usage

tsx
// in src/posts/PostEdit.tsx
import * as React from 'react';
import { Alert } from '@mui/material';
import { Edit, SimpleForm, TextInput, useRecordFromLocation } from 'react-admin';

export const PostEdit = () => {
    const recordFromLocation = useRecordFromLocation();
    return (
        <Edit>
            {recordFromLocation
                ? (
                    <Alert variant="filled" severity="info">
                        The record has been modified.
                    </Alert>
                )
                : null
            }
            <SimpleForm>
                <TextInput source="title" />
            </SimpleForm>
        </Edit>
    );
}

Options

Here are all the options you can set on the useRecordFromLocation hook:

PropRequiredTypeDefaultDescription
searchSourcestring'source'The name of the location search parameter that may contains a stringified record
stateSourcestring'record'The name of the location state parameter that may contains a stringified record

searchSource

The name of the location search parameter that may contains a stringified record. Defaults to source.

stateSource

The name of the location state parameter that may contains a stringified record. Defaults to record.