Skip to content

Question: Is useCache supposed to automatically return the updated cache data after cache change? #2602

@jacobcossman

Description

@jacobcossman

React version (e.g., 18.2.0)
18.2.0

TypeScript version (if used) (e.g., 4.8.2)
4.1.5

Rest Hooks version (e.g., 6.3.11)

@rest-hooks/hooks: "^3.1.13"
@rest-hooks/react: "^7.3.1"
@rest-hooks/rest: "^6.5.0"

Package manager version (e.g., yarn 2, npm 8)
npm 8.19.3

Environment version (e.g., chrome 108, react-native 0.70.6 android)
Latest Chromium (Brave Browser)

Node version (e.g., 16.13.2)
16.13.2

Describe the bug
This seems like a bug but could be a misunderstanding.

In my use case, I want to suspend the initial fetch, but then read from the cache for all future updates. I do not want to re-suspend on data update. Here is how I've written the code:

useSuspense(InternalUserCourseResource.getList, { categoryId: category, userId: authUser?.id })
const courses = useCache(InternalUserCourseResource.getList, { categoryId: category, userId: authUser?.id })

However, courses here is never updated when the course entities are changed in the cache. I have verified this by observing new course entities being added to the state after relevant POST requests. The only way to get the value of courses to update is to call another fetch or invalidate the cache. Doing another fetch is a fine workaround, but I'd rather not hit the server because the data is already in the cache.

Any insight would be appreciated!

To Reproduce
Steps to reproduce the behavior:

  1. Fetch list of entities from endpoint using useSuspense
  2. Get the list of entities from the cache using useCache
  3. Add a new entity via a POST request
  4. Observer that useCache never returns updated data.

Expected behavior
useCache should return new data when the cache is updated.

Additional context
Any console errors? What does the networking inspector show?

No console errors.

Minimum reproducable example or test case
For many cases reproducing the bug is fairly straightforward. However, if
this requires special conditions to trigger, an example can expedite resolution of the issue.
Provide a github repo with usage, or fork rest hooks and add a test case that fails.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions