facebookexperimental / Recoil

Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.
https://recoiljs.org/
MIT License
19.5k stars 1.18k forks source link

urlSyncEffect error handling for query values #2222

Open paulyokuda opened 1 year ago

paulyokuda commented 1 year ago

What is the recommended way to error handle query keys that are synced to recoil atoms via urlSyncEffect? I see we can write a custom Checker which returns a 'success' | 'error' type; however, I'm less sure what the guidance is on how to catch this error?

The use case I have here is that I have a recoil atom with its value synced to the URL. Users might manually try to munge with the URL and hit enter, which could land in an erroneous state.

Example: My atom:

export const exampleAtom = atomFamily<string[], string | null>({
  key: 'exampleAtomKey',
  default: [],
  effects: () => [
    urlSyncEffect({
      refine: (value) => {
        if (!isValidStringArray(value)) {
          return {
            type: 'failure',
            message: 'myItemKey is not a valid string array!',
            path: new Path(),
          };
        }

        return {
          value: value as string[],
          type: 'success',
          warnings: [],
        };
      },
      syncDefault: true,
      itemKey: 'myItemKey',
      storeKey: 'myStoreKey',
    }),
  ],
});

Here I'm using refine to do my typechecking, but where should this 'failure' type be handled? We have ErrorBoundarys that catch what is thrown, but I'm not seeing a first class way to handle refine failure types specifically.

Versions I'm using:


"recoil": "^0.7.6",
"recoil-sync": "^0.2.0",
"@recoiljs/refine": "^0.1.1",

Thanks!
paulyokuda commented 1 year ago

@drarmstr Let me know what you think!