-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix(initialArgs): pass initialState arg to initializer, not internal state #33
fix(initialArgs): pass initialState arg to initializer, not internal state #33
Conversation
I think this change makes sense. A consequence of it is that the following useReducer(reducer, initialState, v => v); which currently would still be useReducerWithSideEffects(reducer, initialState, v => Update(v)); but I think that makes sense and would be expected from folks. 👍! Thanks @chrisdhanaraj ! |
@jamesplease - hmm, are you sure that's the case? Doesn't the mergeState + the return inside the init function here handle actually updating the state? |
Ohhh, I misread what this commit does. You're right, it would still be Would this refactor prevent folks from returning side effects in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignore this lol
Ahhh yes, it would - the goal was to mimic the behavior of the original useReducer. I can def see the positives of being able to load up side effects on the initial render though, maybe I'll just wrap my use case to expose only state |
I think what I've been doing is |
That makes sense. I could make something like that work, I think, but it wouldn't be as clean as what I'm currently doing. Let me explain my use case a little bit more: I have a few reducers that I expose as libraries. So, in an app, one might do: // Under-the-hood this calls useReducerWithSideEffect
import useLibReducer from 'lib-reducer';
const [state, dispatch] = useLibReducer(options); Within these libraries, a pattern I often use is a utility that computes the new state, and the associated side effects, from a set of inputs. So, internally, I might have: function computeNewState(options) { ... }
// => returns { sideEffects, state } For each action type, the reducer ultimately call this util, and so does The previous state, new state, and side effects are so tightly linked that I don't think I'd be able to split this out into separate I'm open to suggestions if you have any thoughts on how this could be better, or if you both think the API in this PR is better either way, then I'm OK with that, too! |
Nah I like yours! I actually thought this was just a goof, not intentional. |
Right now when you use the
initializer
argument, you receiveInstead of the second parameter (initialState) like you would in a normal
useReducer
. This fixes that - I also flipped the logic a little bit in the argument for creating the initState. IMO, putting the early return in there reads cleaner.Also added in a prettier file - I matched the majority style of the file, although there was actually some discrepancy (sometimes trailing commas, sometimes not).