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.

Importing Blocks into a react component

default discord avatar
muhammadmustafaahmaa12 months ago
8

Hi guys, I'm currently trying to create a custom listing page using react and I want to implement the payload

blocks

field into the component. Is there a way to import it like how we import other components such as

DateTimeInput

and

SelectInput

?

  • default discord avatar
    markatomniux12 months ago

    Kind of. Try the RenderFields component from

    payload/components/forms

    ;



    import { RenderFields, fieldTypes } from 'payload/components/forms';
    
    const MyComponent = () => {
    
      return (
        <RenderFields
        fieldTypes={fieldTypes}
        fieldSchema={[
            {
                name: 'title',
                label: 'Title',
                type: 'text',                           
                required: true,
            },
          ]}
        />
      )
    }
  • default discord avatar
    muhammadmustafaahmaa12 months ago

    I'm currently using this one as well, but I have some issue on injecting data into it



    let's say I have a state storing an object like this:


    object = {
      name: 'object',
      segments: [],
    }

    is there a way for me to inject the segments into the RenderFields?



    // Fields
    const fields: Field[] = [
        {
          name: 'segments',
          type: 'blocks',
          admin: {
            initCollapsed: true,
          },
          blocks: [Image, Html],
        },
      ];
    
    // Rendering
    return (
        <>
          <RenderFields
            fieldSchema={fields}
            fieldTypes={fieldTypes}
          />
        </>
      );
  • default discord avatar
    markatomniux12 months ago

    yes, using a useState hook and passing that when the value changes

  • default discord avatar
    muhammadmustafaahmaa12 months ago

    do you have a sample code that i can use as a reference?

  • default discord avatar
    markatomniux12 months ago

    Am I correct in thinking your Segments relate to fields/blocks?

  • default discord avatar
    muhammadmustafaahmaa12 months ago

    yeap, i'm trying to implement blocks field into a react component

  • default discord avatar
    markatomniux12 months ago
    const [fields, setFields] = useState<Field[]>([
        {
          name: 'segments',
          type: 'blocks',
          admin: {
            initCollapsed: true,
          },
          blocks: [Image, Html],
        },
      ]);
    
    useEffect(() => {
      setFields(...)
    }, [something])
    
    function changeBlocks() {
      setFields(...)
    }
    
    return (
        <>
          <RenderFields
            fieldSchema={fields}
            fieldTypes={fieldTypes}
          />
        </>
      );


    Just an example



    if you want to change the fields when a prop is changed, trigger the change in useEffect. If you want to update it on a button click or some other event, run it in a function/const

  • default discord avatar
    muhammadmustafaahmaa12 months ago

    nice! this really help me. thank you so much Mark!

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.