3 ms·
...contrary to the other replies, I think thunks are fine (and have yet to understand why sagas are needed at all). But I have minor issues with createAction a
by Izkata 7y ago
...contrary to the other replies, I think thunks are fine (and have yet to understand why sagas are needed at all). But I have minor issues with createAction and createSlice:
createAction I don't think we'd ever use, and I'd recommend against it - multiple explicit function arguments instead of a payload blob is much easier to understand when reading the code, and the action function should use those arguments to create the blob. Your example here makes sense simply because the blob appears to be a single string value, rather than say, id+value, or even something more complex.
createSlice, to me, appears to be adding a lot of complexity for no apparent benefit. I even had to read through it a second time before I realized extraReducers was the escape hatch to avoid the action=reducer antipattern, but the structure definitely still seems to be promoting it, where actions are defined as part of a set of reducers, then reused elsewhere if need be.
- acemarke 7y agoBy default, `createAction` accepts a single argument, which it then includes as `action.payload.` This requires the least amount of code written to define the action creator, but requires the dispatching code (such as a click handler) to correctly format the payload contents. It aims for the 80% use case by default. However, yes, there are many cases where you might want to accept multiple arguments for an action creator, do additional work to define the contents of the payload, or generate unique IDs as part of creating the action. That's why `createAction` also accepts a "prepare callback" as its second argument [0] [1]. `createSlice`'s primary benefit is to remove the need to write action types and action creators by hand. All you have to do is write your reducer functions, give them meaningful names, and you get the action generated creators for free (and the action types effectively become an invisible implementation detail). We've always told folks that it is not required to have a 1:1 mapping between action types and reducers [2] and that many reducers can respond to a single action [3], but realistically speaking, most actions _are_ only handled by one reducer. Again, `createSlice` handles that 80% use case by generating actions that correspond to that given case reducer in the `reducers` argument, but also allow you to handle the "events" case by listening for other actions in the `extraReducers` argument. (And, the `reducers` argument allows defining "prepare callbacks" as well if desired [4]). So, in both of these APIs, we solve the 80% use case by default, but allow handling the additional 20% if desired. Most folks won't need to be calling `createAction` themselves given the existence of `createSlice`, but there's no reason to _not_ have it as part of the public API (especially since `createSlice` already uses it internally). [0] https://redux-toolkit.js.org/api/createAction#using-prepare-callbacks-to-customize-action-contents https://redux-toolkit.js.org/api/createAction#using-prepare-... [1] https://redux-toolkit.js.org/tutorials/intermediate-tutorial#implementing-todo-ids https://redux-toolkit.js.org/tutorials/intermediate-tutorial... [2] https://redux.js.org/faq/actions#is-there-always-a-one-to-one-mapping-between-reducers-and-actions https://redux.js.org/faq/actions#is-there-always-a-one-to-on... [3] https://redux.js.org/style-guide/style-guide#model-actions-as-events-not-setters https://redux.js.org/style-guide/style-guide#model-actions-a... [4] https://redux-toolkit.js.org/api/createSlice#customizing-generated-action-creators https://redux-toolkit.js.org/api/createSlice#customizing-gen...