Skip to main content

enableOnlyWhenAllKeysTruthy

Description: If true, the action will only be executed when all keys resolve to true.

Default Value: false.

  • How To: This is the preferred option for dependent fetches. Use enableOnlyWhenAllKeysTruthy: true when a fetch should only occur if all parts of its key are truthy. This provides a concise and declarative way to manage dependencies within the key itself.
    // Correct: Fetch todos only if user ID is available in the key
    const { data: user } = useFetch('user', fetchUser); // key, action
    const todosFetch = useFetch(
    ['todos', user?.id], // key contains null if user ID is missing
    () => fetchTodos(user.id),
    { enableOnlyWhenAllKeysTruthy: true } // fetch only when user.id is truthy
    );
  • Best Practice: Avoid enableOnlyWhenAllKeysTruthy: true if you need more granular control over the enabled state, or if some key parts can legitimately be falsy without disabling the fetch. In such cases, the enabled option might be more suitable.