ReactJS Live Queries not triggering function

Hi, I am currently following the Rarible Clone tutorial, and I am using ReactJS as a framework.

I have an issue on the Create event.

Here’s my code:

And if I use this code

subscription.on('create', (object) => {
  console.log('object created');
});

It’s not logging the “object created” but in the server the event is being created.

Check the spelling of your table name. The tutorial repo uses SoldItems. The name in the query must match.

I changed it to ItemSold and it already has values.

Live queries rock so much!! YEAH! I love Moralis more and more! Thank you for providing this awesome tool!

For future readers trying to implement live queries with React using Functional Components, here is my working code:

useEffect(async () => {
    fetchYourData();
    let query = new Moralis.Query("Data");
    let subscription = await query.subscribe();
    subscription.on("create", fetchYourData);
    subscription.on("update", fetchYourData);
    subscription.on("delete", fetchYourData);
  }, []);

This will subscribe to any create, update or delete event of the Data-Table. Yes, you can subscribe to multiple events. I know, it is freaking awesome right!

Place the subscription inside useEffect, make the function async (!), and pass your data-fetching function as a callback to the subscription => BOOOM! there you go! live reloading your sexy React UI. :star_struck:

3 Likes

Thank you so much for this Thomas!

2 Likes