braze-inc / braze-react-native-sdk

Public repo for the Braze React Native SDK
https://www.braze.com
Other
64 stars 84 forks source link

getContentCards returns stale data after calling requestContentCardsRefresh #160

Closed jennifer-dickinson closed 2 years ago

jennifer-dickinson commented 2 years ago

We are currently using our own implementation for content cards UI, because of this we need to manually refresh the cards. However the requestContentCardsRefresh method does not return the value of the api call, so we're required to use the getContentCards method to receive the cards. The issue is, because the requestContentCardsRefresh method is not await-able, we are receiving the previously cached set of cards. This leaves us with stale data, despite having pulled the most recent set of cards.

This can be reproduced by

  braze.requestContentCardsRefresh(); // this call is not await-able, but performs the asynchronous api call
  const cards = await braze.getContentCards(); // this call receives the stale data
wesleyorbin commented 2 years ago

Hi @jennifersalas. You can create a listener for the Braze.Events.CONTENT_CARDS_UPDATED event that will be fired after the refresh completes. You can call getContentCards() inside the listener's callback to update your cards to the most recent set.

braze.addListener(Braze.Events.CONTENT_CARDS_UPDATED, cards => {
  this.cards = braze.getContentCards();
});
braze.requestContentCardsRefresh();