-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Reducer Hook's lazy init API #14723
Conversation
For posterity what’s the actual Flow signature with overload that we intend? |
Still requires an `any` cast in the case where `init` function is not provided.
declare function useReducer<S, A>(
reducer: (S, A) => S,
initialState: S,
): [S, Dispatch<A>];
declare function useReducer<S, A>(
reducer: (S, A) => S,
initialState: S,
init: void,
): [S, Dispatch<A>];
declare function useReducer<S, I, A>(
reducer: (S, A) => S,
initialArg: I,
init: (I) => S,
): [S, Dispatch<A>]; |
@acdlite I think we should also add an overload that accepts void as a third arg. That way it allows undefined to be passed. That’s useful when you’re forwarding arguments in wrappers like we do. |
@sebmarkbage Good call, I edited my comment accordingly |
declare function useReducer<S, A>(
reducer: (S, A) => S,
initialState: S,
init: void,
): [S, Dispatch<A>]; What use case is this variation intended for? |
Probably:
Like if you wrap |
Yeah maybe that's it. I wouldn't initially have expected that to require its own explicit variation. |
Effectively, this acts as if the third argument defaults to the identity function? 🤔 declare function useReducer<S, A>(
reducer: (state: S, action: A) => S,
initialState: S,
initializer?: undefined
): [S, Dispatcher<A>]
declare function useReducer<S, A, I>(
reducer: (state: S, action: A) => S,
initializerArg: I,
initializer: (arg: I) => S
): [S, Dispatcher<A>] |
* Improve Reducer Hook's lazy init API * Use generic type for initilizer input Still requires an `any` cast in the case where `init` function is not provided.
Summary: React is updating the signature for the `useReducer` hook facebook/react#14723 This PR reflects those changes. We'll also update the official react docs soon. I'm doing a build and tests right now locally (installing everything from scratch atm), but figured I'd start the PR as well. Pull Request resolved: #7420 Reviewed By: bvaughn Differential Revision: D13928051 Pulled By: jbrown215 fbshipit-source-id: 52da18fb52a25ec3b8353696df94cbeb7992e5ef
* Improve Reducer Hook's lazy init API * Use generic type for initilizer input Still requires an `any` cast in the case where `init` function is not provided.
No description provided.