useFieldValue
A hook that gets the value of a field of the current record. It gets the current record from the context or use the one provided as a prop. It supports deep sources such as name.fr
.
Here is an example TextField
component:
// In TextField.tsximport * as React from 'react';import { useFieldValue } from 'ra-core';
export const TextField = (props) => { const value = useFieldValue(props); return <span>{value}</span>;}
// In PostShow.tsximport { ShowBase } from 'ra-core';import { TextField } from './TextField.tsx';
export const PostShow = () => ( <ShowBase> <div> <TextField source="author.name" label="Author" /> </div> </ShowBase>);
Params
Section titled “Params”source
Section titled “source”The name of the property on the record object that contains the value to display. Can be a deep path.
import * as React from 'react';import { useFieldValue } from 'ra-core';
export const CustomerCard = () => { const firstName = useFieldValue({ source: 'firstName' }); const lastName = useFieldValue({ source: 'lastName' }); return <span>{lastName} {firstName}</span>;}
record
Section titled “record”The record from which to read the value. Read from the RecordContext
by default.
import * as React from 'react';import { useFieldValue, useGetOne } from 'ra-core';
export const CustomerCard = ({ id }: { id: string }) => { const { data } = useGetOne('customer', { id }); const firstName = useFieldValue({ source: 'firstName', record: data }); const lastName = useFieldValue({ source: 'lastName', record: data }); return <span>{lastName} {firstName}</span>;}
defaultValue
Section titled “defaultValue”The value to return when the record does not have a value for the specified source
.
import * as React from 'react';import { useFieldValue } from 'ra-core';
export const CustomerStatus = () => { const status = useFieldValue({ source: 'status', defaultValue: 'active' }); return <span>{status}</span>;}