UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

41 lines 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useRecordContext = void 0; var react_1 = require("react"); var RecordContext_1 = require("./RecordContext"); /** * Hook to read the record from a RecordContext. * * Must be used within a <RecordContext> such as provided by the <EditContextProvider> * (e.g. as a descendent of <Edit> or <EditBase>) or within a <ShowContextProvider> * (e.g. as a descendent of <Show> or <ShowBase>) * * @example // basic usage * * import { useRecordContext } from 'ra-core'; * * const TitleField = () => { * const record = useRecordContext(); * return <span>{record && record.title}</span>; * }; * * @example // allow record override via props * * import { useRecordContext } from 'ra-core'; * * const TitleField = (props) => { * const record = useRecordContext(props); * return <span>{record && record.title}</span>; * }; * render(<TextField record={record} />); * * @returns {RaRecord} A record object */ var useRecordContext = function (props) { // Can't find a way to specify the RecordType when CreateContext is declared // @ts-ignore var context = (0, react_1.useContext)(RecordContext_1.RecordContext); return (props && props.record) || context; }; exports.useRecordContext = useRecordContext; //# sourceMappingURL=useRecordContext.js.map