When creating an Async Action, avoid updating state stores directly inside the action function. If an action hits a cached value, the action body will not re-run, causing your stores to become out of sync with the cached data.
Instead, use the postActionHook option. This hook is guaranteed to run after every action completion, regardless of whether the action was executed fresh or retrieved from the cache. This ensures your application state (e.g., view data, organized results) remains consistent.
Note on stores: The stores object is only available in the hook if you are using <PullstateProvider> for server-side rendering. For client-side only applications, you should import and update your stores directly within the hook.
const searchPicturesForTag = PullstateCore.createAsyncAction(
async ({ tag }) => {
const result = await PictureApi.searchWithTag(tag);
if (result.success) {
return successResult(result);
}
return errorResult([], `Couldn't get pictures: ${result.errorMessage}`);
},
{
postActionHook: ({ result, stores }) => {
if (!result.error) {
// For SSR, use stores. For client-side, import your store directly.
stores.GalleryStore.update(s => {
s.pictures = result.payload.pictures;
});
}
},
}
);