react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

defaultProps recommended for label of custom field

Open hco opened this issue 9 months ago • 4 comments

On https://marmelab.com/react-admin/Fields.html#writing-your-own-field-component, it is recommended to use defaultProps for having a default label for a custom field.

As defaultProps for function components do not exist anymore, another way should be recommended.

Other information: Mention on defaultProps being removed: https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-deprecated-react-apis

hco avatar Apr 18 '25 12:04 hco

Thanks for pointing this. I'm marking it as a documentation issue.

We won't need defaultProps for labellingcustom fields in a data grid with the new DataTable component. We will need to use the same approach for the Show layouts, and then we can stop recommending setting the label on Field components altogether.

fzaninotto avatar Apr 18 '25 15:04 fzaninotto

I've updated the documentation to remove the defaultProps pattern for custom field components. The changes include:

  1. Updated the FullNameField example in Fields.md to use parameter destructuring with source and ...props instead of label and ...props
  2. Updated the ColoredNumberField example in SX.md to: Remove the defaultProps assignment Use parameter destructuring with source and ...props Pass the source prop explicitly to the underlying component Should I proceed with a PR ?

dhfra21 avatar Apr 25 '25 13:04 dhfra21

As explained in #10699, what you propose doesn't work.

fzaninotto avatar Apr 25 '25 14:04 fzaninotto

got it! i'll look for another solution.

dhfra21 avatar Apr 25 '25 14:04 dhfra21