Skip to content

Can I use this with React Query v4 and v5?

Yes! This library works with all versions of React Query (TanStack Query) because it only generates key arrays, which is the standard format.

How do I handle optional parameters?

You can use optional properties in your parameter types:

typescript
const keys = createKeyFactory('app', {
  users: {
    search: (params: { query: string; page?: number }) => [
      params.query,
      ...(params.page ? [params.page.toString()] : []),
    ],
  },
});

Can I use this with other query libraries?

Yes! While designed for React Query, this library generates standard key arrays that work with any library that uses array-based keys.

How do I handle special characters in keys?

Special characters are supported in key names:

typescript
const keys = createKeyFactory('app', {
  'key-1': () => ['value'],
  'key_2': () => ['value2'],
});

keys['key-1']({}) // Works fine

What's the difference between array shorthand and functions?

Array shorthand is syntactic sugar for functions that return static arrays:

typescript
// These are equivalent:
list: ['all']
list: () => ['all']

Use functions when you need dynamic values based on parameters.

Can I nest keys infinitely?

Yes, there's no limit to nesting depth. However, keep it reasonable for maintainability.

How do I migrate from manual key management?

  1. Create a key factory with your existing key structure
  2. Replace manual key arrays with factory calls
  3. TypeScript will help catch any mistakes during migration

Does this work with React Query's queryKeyHashFn?

Yes! The factory generates standard arrays that work with all React Query features, including custom hash functions.

Released under the MIT License.