Skip to content
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

Merged
merged 2 commits into from
Jan 30, 2019

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Jan 30, 2019

No description provided.

@gaearon
Copy link
Collaborator

gaearon commented Jan 30, 2019

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.
@acdlite
Copy link
Collaborator Author

acdlite commented Jan 30, 2019

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>];

@sebmarkbage
Copy link
Collaborator

@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.

@acdlite
Copy link
Collaborator Author

acdlite commented Jan 30, 2019

@sebmarkbage Good call, I edited my comment accordingly

@acdlite acdlite merged commit ba6477a into facebook:master Jan 30, 2019
@bvaughn
Copy link
Contributor

bvaughn commented Jan 30, 2019

declare function useReducer<S, A>(
  reducer: (S, A) => S,
  initialState: S,
  init: void,
): [S, Dispatch<A>];

What use case is this variation intended for?

@gaearon
Copy link
Collaborator

gaearon commented Jan 30, 2019

Probably:

That way it allows undefined to be passed. That’s useful when you’re forwarding arguments in wrappers like we do.

Like if you wrap useReducer from higher level API that may or may not receive an init argument I guess?

@bvaughn
Copy link
Contributor

bvaughn commented Jan 30, 2019

Yeah maybe that's it. I wouldn't initially have expected that to require its own explicit variation.

@Jessidhia
Copy link
Contributor

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>]

n8schloss pushed a commit to n8schloss/react that referenced this pull request Jan 31, 2019
* 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.
facebook-github-bot pushed a commit to facebook/flow that referenced this pull request Feb 6, 2019
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
This was referenced Sep 20, 2019
NMinhNguyen referenced this pull request in enzymejs/react-shallow-renderer Jan 29, 2020
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants