Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Access Document data in admin ui component?

default discord avatar
eddy_solarxlast year
8

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>
  );
};
  • default discord avatar
    ritsu0455last year

    yeah, possible


    see

    https://payloadcms.com/docs/admin/hooks#useallformfields
    useField

    - access to 1 field


    useFormFields

    - acess to fields with selector


    useAllFromFields

    - just all fields



    note in 3.0 they are exported in

    @payloadcms/ui
  • default discord avatar
    eddy_solarxlast year

    Is a little hidden but its under


    import { useFormFields } from "@payloadcms/ui/forms/Form";
  • default discord avatar
    simka0997last year
    @423216344302092288

    @79302304150331392

    🙏



    @423216344302092288

    @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?

  • default discord avatar
    ritsu0455last year
    fetch

    them in

    useEffect
  • default discord avatar
    simka0997last year
    @423216344302092288

    I was hoping for a hack where I can simply adjust the depth before doing the above, but this works too. Thanks a lot 🙂

  • default discord avatar
    techinvertedlast year

    Hey

    @823927930874363905

    , have you figured this out? How do I push updates to other fields from a UI button field?

  • default discord avatar
    simka0997last year
    @149873958760480769

    sorry for the late response. I’m using useEffect to fetch the doc with depth of 2 now.

  • default discord avatar
    techinvertedlast year

    Okay thanks

    @823927930874363905
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.