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 useEffect in field condition

default discord avatar
superwafflepuffs2 years ago

Is is possible to access useEffect in a field condition?


I'm trying to build an attribute manager and my current use case is an array field that takes 2 fields:


1. Relationship to predefined attribute (ex. size, color, etc...)


2. An input of type: text, number, select, informed by the type returend from the attribute relationship



Right now the problem is sibling data only returns the document id for my relationship, but I need to access the relationship values:


{
      name: 'attributes',
      type: 'array',
      fields: [
        {
          name: 'type',
          type: 'relationship',
          relationTo: 'attributes',
          required: true
        },
        {
          name: 'value',
          type: 'text',
          required: true,
          admin: {
            condition (data, siblingData) {
              // this loops out of control without useEffect, but I can't seem to get useEffect into scope here...
              useEffect(() => {
                (async () => {
                  const type = await fetch(
                    `${process.env.PAYLOAD_PUBLIC_SITE_URL}${process.env.PAYLOAD_PUBLIC_API_ROUTE}/attributes/${siblingData.type}`
                  );
                  console.log(type);
                })();
              }, [siblingData.type]);

              return true
            }
          }
        }
      ]
    },


Ended up just using a custom field for my use case.

    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.