How to dispatch actions in Components
See original GitHub issueAs 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:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
yes.
The idiomatic way is to get a callback with useStore.
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