Using `relationship` field for `RowLabel`

default discord avatar
slavanossarlast year
2

I have an

array

field that lets the user select an item from a collection with a

relationship

field. I'd like to use the

title

field of that item for the

RowLabel

, but the

relationship

field returns the item

id

. I've seen that there's a Local API that can be used to asynchronously retrieve data (e.g. with

findById

), but

RowLabel

doesn't accept

Promise

as a return value.



Is there an easy way to do this?



Sorry just found this question, seems to answer my question

https://discord.com/channels/967097582721572934/1097588914732150865/1097608591793455164


For anyone else who comes across this, a solution that uses

fetch

instead of

axios

admin: {
  components: {
    RowLabel: ({ data, index = 0 }) => {
      const [label, setLabel] = useState(`Feature ${String(index).padStart(2, '0')}`)
      
      useEffect(() => {
        fetch(
          `${process.env.PAYLOAD_PUBLIC_SITE_URL}${process.env.PAYLOAD_PUBLIC_API_ROUTE}/features/${data.feature}`
        ).then(async (res) => {
          setLabel((await res.json()).title)
        })
      }, [data.feature])

      return label
    },
  },
},
  • default discord avatar
    jm.svlast year

    just saved me, thanks. This would be a great add to the

    rowLabel

    docs. Or even better adding a

    depth

    field parameter somewhere in

    admin

    to do this automatically

  • default discord avatar
    jakey___11 months ago

    This also helped me. thanks @slavanossar !

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.