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


Top comments (2)

Collapse
 
asma_745 profile image
Asmaa Abdelfattah โ€ข

๐Ÿ‘๐Ÿ‘๐Ÿค๐Ÿค ููˆู‚ ุงู„ุนุธู…ุฉ

Collapse
 
abdoseadaa profile image
abdelrahman seada โ€ข โ€ข Edited

ุฌูŠุดูŠ

Image description

Sentry image

See why 4M developers consider Sentry, โ€œnot bad.โ€

Fixing code doesnโ€™t have to be the worst part of your day. Learn how Sentry can help.

Learn more

๐Ÿ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someoneโ€™s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay