Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is the cache global?


It's up to you. I intentionally kept moving parts to the minimum in my demo, but here's the real thing I was using behind the scenes: https://github.com/facebook/react/tree/master/packages/simpl.... So in that sense it's not global, you can create as many as you like and then isolate them (e.g. per subtree).

The `simple-cache-provider` is just a reference implementation though. It's somewhat naïve, e.g. it doesn't have invalidation strategy and just lets you replace the whole cache with a new one if you want to invalidate.

We'll publish more details on lower level API and how to write your own cache in the future.


It seems from https://github.com/facebook/react/blob/master/packages/simpl... that in the live version you'll be specifying which cache to use; this could be global, provided via React's old or new Context APIs, or passed down through props. And you could always write your own fetcher factory if you want to do something really custom; there's nothing special about the SimpleCacheProvider, and it doesn't use any React internals.


FWIW my demo code looked like

    export let cache;
    function initCache() {
      cache = createCache(initCache);
    }
    initCache();

    export function createFetcher(fetch) {
      const res = createResource(fetch);
      return {
        read(...args) {
          return res(cache, ...args);
        },
      };
    }




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: