Get updated list of objects from react hook after adding new items to the database

Hello guys,

I have a question regarding react-moralis - After adding new custom objects to the a list of custom objects in the database, I want to fetch an updated list of objects with the newly added item to render it on the screen.

For now I fetch the the custom objects related to the current user with a cloud function, and I receive the respected values in the data object

const { data, error, isLoading } = useMoralisCloudFunction(

Then I work with the data object - but if more items are added data in the database, the data object does not return these additional values.
I couldn’t figure out how to fetch an updated version of data with useMoralisCloudFunction() so, I tried with live queries to fetch new item(s) like this, add then add it to the existing list of objects:

useMoralisSubscription("CustomObject", q => q, [], {
    onCreate: data => {
      const newItem = data;
      // this consoles me the new item correctly
      // but can't setState in the subscription hook
      setObjectArray([...objectArray, newItem])

Whats the best way to get an updated array / state after adding new CustomObjects to the database to query ? Many thanks in advance :slight_smile: