Components

useRecord

<useRecord />

Constructor

# <useRecord />

A powerful, hook which allows you to manage an entire record of given type.

Take a look of creating a component which renders form for some non-existing record. Form have name and surname fields. After clicking "save" user will create a new record. Consecutive calls will update it.

import { BasePropertyComponent, useRecord, Box, useTranslation } from 'admin-bro'

const MyRecordActionComponent = (props) => {
  const { record: initialRecord, resource, action } = props

  const { record, handleChange, submit } = useRecord(initialRecord, resource.id)
  const { translateButton } = useTranslation()

  const nameProperty = resource.editProperties.find((property) => property.name === 'name')
  const surnameProperty = resource.editProperties.find((property) => property.name === 'surname')

  const handleSubmit = (event) => {
    submit().then(() => {
       // do something
    })
  }

  return (
    <Box
      as="form"
      onSubmit={handleSubmit}
    >
      <BasePropertyComponent
        where="edit"
        onChange={handleChange}
        property={nameProperty}
        resource={resource}
        record={record}
      />
      <BasePropertyComponent
        where="edit"
        onChange={handleChange}
        property={surnameProperty}
        resource={resource}
        record={record}
      />
      <Button variant="primary" size="lg">
        {translateButton('save', resource.id)}
      </Button>
    </Box>
  )
}
export default MyRecordActionComponent

Returns UseRecordResult.

View Source admin-bro/src/frontend/hooks/use-record.tsx, line 67

Type Definitions

object

# UseRecordResult

Result of useRecord hook

Properties:
Name Type Description
record RecordJSON

recordJSON instance for given resource.

handleChange function

Function compatible with onChange method supported by all the components wrapped by, BasePropertyComponent.

submit function

Triggers submission of the record. Returns a promise., If custom params are given as an argument - they are merged, to the payload.

loading boolean

Flag indicates loading.

progress number

Upload progress

View Source admin-bro/src/frontend/hooks/use-record.tsx, line 143