atomico icon indicating copy to clipboard operation
atomico copied to clipboard

New api for Store based on Hooks

Open UpperCod opened this issue 3 years ago • 0 comments

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.

UpperCod avatar Aug 22 '22 13:08 UpperCod