question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to dispatch actions in Components

See original GitHub issue

As I am learning zustand, I really like it. It is simple, lightweight and super fast. So I am trying a new project and I have a question.

How can i fetch actions via UI?

This is my orderStore file

const orderState = create((set, get) => ({
  orders: [],
  getOrders: async (orders) => {
    try {
      set({ orders });
      return get().orders;
    } catch (error) {
      throw error?.response.data || "An Error Occured";
    }
  },
}));

export default orderState;

And I want to call this action in Component ( .tsx ) How can i do that? If I call within useEffect it say Hooks Error.

const getOrders = orderState((state) => state.getOrders);

Please Help Me.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
dai-shicommented, Jul 8, 2021

yes.

The idiomatic way is to get a callback with useStore.

const useOrderStore = create((set) => ({
  orders: [],
  getOrders: async () => {
    try {
      const orders = await fetchOrders();
      set({ orders });
      return orders;
    } catch (error) {
      throw error?.response.data || "An Error Occured";
    }
  },
}));

const Component = () => {
  const getOrders = useOrderStore(state => state.getOrders);
  useEffect(() => {
    getOrders().then(orders => console.log(orders));
  }, [getOrders])
  // ...
}
1reaction
dai-shicommented, Jul 10, 2021

It works even if it’s nested. The caveat is that you need to merge when updating the state. The library only shallow merges it.

https://github.com/pmndrs/zustand/blob/896554237eb1c258bc20e0c87abe9eba65282a66/src/vanilla.ts#L68

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect: Dispatching Actions with mapDispatchToProps
Connect: Dispatching Actions with mapDispatchToProps · By default, a connected component receives props.dispatch and can dispatch actions itself.
Read more >
Different Ways to Dispatch Actions with Redux - Pluralsight
In this guide, we'll look at different ways of dispatching the action and at isolating the components from Redux.
Read more >
4 ways to dispatch actions with Redux - Bam Tech
1) Simply pass the dispatch method to your component. The dispatch method is a method of the store object. An action is dispatched...
Read more >
React-Redux Dispatch Action from Component - Stack Overflow
I'm trying to dispatch an action from a button click. I've created my component. I connect it to the store that is passed...
Read more >
A special way to dispatch Redux Actions — React-Redux
1. This is a simple example of dispatching actions from React class component that most of us use at the early stages of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found