I have my ui component and I want to access data from the current collection document, is it possible?
// Current Code
{
name: "button",
label: "Update Transactional Data",
type: "ui",
admin: {
components: {
Field: MyCustomUIField,
},
},
},
"use client";
export const MyCustomUIField: React.FC<{ path: string }> = ({
path,
...rest
}) => {
const data = useData(); // I was expecting a hook or something similar.
return (
<div className="mb-4">
<button type="button">Update Data</button>
</div>
);
};
yeah, possible
see
https://payloadcms.com/docs/admin/hooks#useallformfieldsuseField
- access to 1 field
useFormFields
- acess to fields with selector
useAllFromFields
- just all fields
note in 3.0 they are exported in
@payloadcms/ui
Is a little hidden but its under
import { useFormFields } from "@payloadcms/ui/forms/Form";
@79302304150331392
🙏
@79302304150331392
I'm getting the data like this:
const [fields] = useAllFormFields();
const fieldsData = reduceFieldsToValues(fields, true);
Any idea how I can also populate the relationship fields to not only get the ID of the related doc?
fetch
them in
useEffect
I was hoping for a hack where I can simply adjust the depth before doing the above, but this works too. Thanks a lot 🙂
Hey
@823927930874363905, have you figured this out? How do I push updates to other fields from a UI button field?
sorry for the late response. I’m using useEffect to fetch the doc with depth of 2 now.
Okay thanks
@823927930874363905Star
Discord
online
Get dedicated engineering support directly from the Payload team.