Can't update array value programatically?

default discord avatar
derosul
3 months ago
8

Hi all, I'm trying to update an array field from another custom UI component I'm creating.



The field definition:



{
  name: 'tickers',
  type: 'array',
  fields: [
    {
      name: 'symbol',
      type: 'text'
    }
  ],
}


How I'm trying to update the field's value:



const myField = useField({ path: 'tickers' });
myField.setValue([ { symbol: 'test' } ]);


Sadly enough nothing happens, and no error is thrown.


However... when logging

myField.value

it logs the array length of the field, not the actual value itself.



Something else I tried:


const [ allFields, dispatchFields ] = useAllFormFields();
dispatchFields({ type: 'UPDATE', path: 'tickers', value: [{ symbol: 'test' }] })


But sadly... no luck there.



Bump 👀



@jmikrut here you go

  • discord user avatar
    jmikrut
    Payload Team
    3 months ago

    nice



    will cross-post my answer here:



    one note, we are about to release changes that make it a LOT easier to programmatically update arrays


    but in the meantime, i wrote up an example for how to do it here:


    https://github.com/payloadcms/payload/issues/2427#issuecomment-1512320542
  • default discord avatar
    derosul
    3 months ago

    Ah, in the meantime I'll try that! thanks @jmikrut 🙂

  • discord user avatar
    jmikrut
    Payload Team
    3 months ago

    of course! and keep an eye out, lots of new arary stuff coming soon

  • default discord avatar
    derosul
    3 months ago

    Sweet! can't wait

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.