DEV Community

Cover image for # πŸ—ƒοΈ Dolab β€” A Lightweight Data-Fetching Hook with Zustand Superpowers ⚑
abdelrahman seada
abdelrahman seada

Posted on

6 4 4 4 4

# πŸ—ƒοΈ Dolab β€” A Lightweight Data-Fetching Hook with Zustand Superpowers ⚑

Managing API calls and shared data in React doesn't have to be a hassle. Dolab simplifies this process with a clean, reusable hook powered by Zustand. It's lightweight, fast, and designed for modern frontend development.

🧠 β€œDolab” is the Arabic word for "Drawer" β€” and like a drawer, it stores, fetches, and shares your data neatly. 🧠


πŸš€ Why Use Dolab?

  • βœ… Minimal API surface
  • πŸ” Auto-refetch with dependency array
  • πŸ“¦ Shared data store across components
  • ⏳ Optional cache expiration (lifetime)
  • πŸ’‘ TypeScript-first design
  • ⚑ Built on Zustand (no context, no hassle)

πŸ“₯ Installation

npm install dolab
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Basic Usage Example

import { SetDolabData, useDolab } from "dolab";

type Todo = { userId: number; id: number; title: string; completed: boolean };

const fetch = async (setData: SetDolabData<Todo[]>) => {
  const res = await fetch("https://jsonplaceholder.typicode.com/todos");
  setData(await res.json());
};

export default function Todos() {
  const { data: todos, loading, triggerRefetch } = useDolab<Todo[]>({
    id: "todos", fetch,
  });

  if (!todos) return (
    <button onClick={triggerRefetch}>
      {loading ? "Loading..." : "Load Todos"}
    </button>
  );

  return (
    <ul>{todos.map(todo => <li key={todo.id}>{todo.title}</li>)}</ul>
  );
}

Enter fullscreen mode Exit fullscreen mode

🧠 Note: The data is not just fetched and displayed β€” it’s also persisted inside Dolab’s internal store, powered by Zustand. This means you can reuse the same data anywhere across your application without triggering another API call.

To access this stored data in other components, simply use the useDolabData hook and pass in the same id used earlier ("todos" in this case):

const {data : todos}= useDolabData<Todo[]>({ id: "todos" });

πŸ”„ Important: This works only if useDolab({ id, fetch }) was already called before useDolabData in the application lifecycle. That call is what fetches and stores the data initially. From there, any component can subscribe to and reactively use the shared data without duplication or redundant requests.

πŸ” Triggering a Manual Refetch:

You can also manually refetch the stored data when needed β€” for example, after submitting a form or on a button click. Simply destructure triggerRefetch from useDolabData:

const { triggerRefetch: refetch } = useDolabData<Todo[]>({ id: "todos" });

// Example: Trigger refetch on button click
<button onClick={refetch}>Refresh Todos</button>

This will re-invoke the fetch function defined in useDolab for the same id and update the stored data accordingly.

πŸ§ͺ Live Demo

Want to try it out? Here’s a live playground with examples and integration demos:

πŸ‘‰ Open on CodeSandbox


🧠 Best Practices

  • Use useDolab once to fetch and store the data.
  • Use useDolabData in child components to access the shared data.
  • Customize lifeTime to control cache expiry.
  • Pass deps to auto-refetch on param/state changes.

πŸ“¦ npm Package

πŸ‘‰ View on npm

πŸ“₯ npm install dolab


✍️ Author


DevCycle image

Fast, Flexible Releases with OpenFeature Built-in

Ship faster on the first feature management platform with OpenFeature built-in to all of our open source SDKs.

Start shipping

Top comments (2)

Collapse
 
asma_745 profile image
Asmaa Abdelfattah β€’

πŸ‘πŸ‘πŸ€πŸ€ ΩΩˆΩ‚ Ψ§Ω„ΨΉΨΈΩ…Ψ©

Collapse
 
abdoseadaa profile image
abdelrahman seada β€’ β€’ Edited

جيشي

Image description

Everything you need, nothing you don\

Everything you need, nothing you don't

Kinde fits into your stack with modern auth, access, and billing built for teams who care about code quality and velocity.

Get a free account

πŸ‘‹ Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creatorsβ€”let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay