dai-shi / react-tracked

State usage tracking with Proxies. Optimize re-renders for useState/useReducer, React Redux, Zustand and others.
https://react-tracked.js.org
MIT License
2.73k stars 72 forks source link

Shared actions in react-tracked? #25

Closed MiloshN closed 5 years ago

MiloshN commented 5 years ago

Is it possible and how, to create actions in react-tracked folder structure? Redux pattern?

Points are to create action and share them between components. In the documentation, are action only in one component?

4nte commented 5 years ago

Actions are just plain javascript objects like{ type: 'foo' } or { type: 'foo', payload: { bar: 1 } }.

It's up to you how/where you declare their types and/or create them. They work exactly the same as in Redux or useReducer hook. React-tracked does not have any difference when it comes to handling actions that I'm aware of, except dispatching objects from state (but that is outside the scope of your question).

You could import an action / action type / action creator from another file, hardcode it in a dispatch function call (dispatch({type: 'foo', payload: {...} })) or any other approach possible in Javascript.

dai-shi commented 5 years ago

Yes, it is possible. You can use Redux pattern if you like.

My proposition (or one of my propositions at this moment) is to use custom hooks to dispatch actions. See the blog post and the code.

dai-shi commented 5 years ago

As a side note, I like to use custom hooks in React Redux too: https://blog.axlight.com/posts/react-hooks-oriented-redux-coding-pattern-without-thunks-and-action-creators/

dai-shi commented 5 years ago

Let me close this issue. @MiloshN Feel free to reopen it if you have questions related to this.