atomicojs / atomico

Atomico a micro-library for creating webcomponents using only functions, hooks and virtual-dom.
https://atomicojs.dev
MIT License
1.15k stars 43 forks source link

New api for Store based on Hooks #88

Open UpperCod opened 2 years ago

UpperCod commented 2 years ago

Describe the solution you'd like

What if we use hooks inside a state manager? 🤔... Imagine reusing custom Hooks even in the Store, example:

import { createStore } from "@atomico/hooks/store";
import { usePromise } from "@atomico/hooks/use-promise";

const store = createStore(
  {
    products: {},
  },
  {
    getProducts(store) {
      const [result, status] = usePromise(() => myApi(`/products`));

      return {
        products: {
          result,
          status,
        },
      };
    },
  }
);

store.actions.getProducts();

const { result, status } = store.state.products;

Additional context

The goal of this API is to reuse customHooks to compose states at the Store level.

✍️ All positive or negative opinions about this proposal are welcome.