problem with dispatchField - ADD_ROW doesn't show up and cause error when saving

default discord avatar
iamPrompt
4 months ago
17

I have a problem recently that I try to use dispatchField with the type

ADD_ROW

but when the function is execute,



const [fields, dispatchField] = useAllFormFields()

dispatchField({
  type: 'ADD_ROW',
  path: '__path__',
  rowIndex: 0,
  subFieldState: {
    provider: {
      initialValue: '',
      value: __value__,
      valid: true,
    },
  },
})


I think the state has been updated but there is no row being add on the dash board and also cause an error on saving draft or publishing.



[10:43:44] ERROR (payload): There was an error while creating a draft [object Object] with ID xxxxxxx.
[10:43:44] ERROR (payload): _shows_versions validation failed: version.__path__: Cast to embedded failed for value "1" (type number) at path "__path__" because of "ObjectParameterError"
[10:43:44] ERROR (payload): TypeError: Cannot read properties of undefined (reading 'version')


The workaround I do now is to manually add the row and then deleted the manually created one before saving which I think it is the additional task.



I don't know if there are any solution to solve this bug?

  • discord user avatar
    jacobsfletch
    Payload Team
    4 months ago

    @iamPrompt try to use the

    useFormFields

    hook instead, it's much more performant and might easier to work with.

    const { value } = useFormFields(([fields, dispatch]) => fields[path])


    Here's some related docs, too:

    https://payloadcms.com/docs/admin/hooks#useformfields
  • default discord avatar
    iamPrompt
    4 months ago

    @jacobsfletch I think, to get the value is fine but the way to dispatch or create a row in an array in programming way is the real problem. I'm not sure that there might be some different in

    dispatch

    function or not by using

    useFormFields

    .


    I have a few fields to change, I call dispatchField once at a time, everything fine, but the array one causing an error.



    As I saw in Array field-types

    https://github.com/payloadcms/payload/blob/master/src/admin/components/forms/field-types/Array/index.tsx#L102

    when adding row in the array, there is a call to

    dispatchRows

    immediately after

    dispatchFields

    execute. I'm not sure whether

    dispatchRows

    is a thing, if so I have tried to find the way to access it but not successful as it might depending on its own fields. What I call is from other fields to set the value.

    image.png
  • discord user avatar
    jmikrut
    Payload Team
    4 months ago

    @iamPrompt yes, to add a row to an array you also need to update that array field's internal row state. @zubricks I think we should write a blog post about how to do this

  • default discord avatar
    iamPrompt
    4 months ago

    @jmikrut is there a quick solution how to do this?

  • discord user avatar
    jesschow
    Payload Team
    3 months ago

    @jmikrut how do we update the array field's internal row state?

  • discord user avatar
    jmikrut
    Payload Team
    3 months ago

    we could certainly build a utility to make this easier but right now it would take some manual work

  • default discord avatar
    iamPrompt
    3 months ago

    Any idea about this??

  • default discord avatar
    Pavel
    2 months ago

    @jmikrut any info on how to deal with this?

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    not yet - but we can write something up soon. in the meantime, i'd say you should look at our internal Array field for some ideas. There are two states that need to be kept up to date - the

    rows

    reducer, and then of course your fields themselves

  • default discord avatar
    Pavel
    2 months ago

    Hope you will do something soon with this. I think it should be made easy to use.

  • default discord avatar
    Grandnainconnu
    3 weeks ago

    Any update on this?

  • discord user avatar
    jmikrut
    Payload Team
    3 weeks ago

    actually, yes, I have written up a response to someone else about how to update array fields:



    https://github.com/payloadcms/payload/issues/2427


    their original issue deals with nested arrays, so my example is more complex, but this should give you an idea

  • default discord avatar
    Grandnainconnu
    3 weeks ago

    Thank you!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

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