0

When I get the data (view) from useStore, I have to write all the way to this (view: myStore.menu.view) and still wrap it all in useObserver. Is there a way to shorten the code, but still keep the logic the same? I use Mobx and React Hooks.

Thanks in advance!

function useBasketStore() {
  const { myStore } = useStore(['exampleStore']);

  return useObserver(() => ({
    view: myStore.menu?.view,
  }));
}

const BasketScreen = () => {
  const { view } = useBasketStore();
......
}
EmilM
  • 107
  • 1
  • 3
  • 11

1 Answers1

0

I think no way. Only the case if your component wrapped by observer. Then you can just use data:

function useBasketStore() {
  const { myStore } = useStore(['exampleStore']);

  return {
    view: myStore.menu?.view,
  };
}

const BasketScreen = () => {
  const { view } = useBasketStore();
......
}

export default observer(BasketScreen)
IT's Bruise
  • 824
  • 5
  • 11