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

Projects
None yet
6 participants
@acdlite
Copy link
Member

acdlite commented Jan 30, 2019

No description provided.

@gaearon

This comment has been minimized.

Copy link
Member

gaearon commented Jan 30, 2019

For posterity what’s the actual Flow signature with overload that we intend?

Use generic type for initilizer input
Still requires an `any` cast in the case where `init` function is
not provided.
@acdlite

This comment has been minimized.

Copy link
Member 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

This comment has been minimized.

Copy link
Member

sebmarkbage commented Jan 30, 2019

@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

This comment has been minimized.

Copy link
Member 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

1 check passed

ci/circleci Your tests passed on CircleCI!
Details
@bvaughn

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Contributor

Jessidhia commented Jan 31, 2019

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 added a commit to n8schloss/react that referenced this pull request Jan 31, 2019

Improve Reducer Hook's lazy init API (facebook#14723)
* 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 added a commit to facebook/flow that referenced this pull request Feb 6, 2019

[PR] [react] New useReducer signature
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

Kiku-git added a commit to Kiku-git/react that referenced this pull request Feb 10, 2019

Improve Reducer Hook's lazy init API (facebook#14723)
* 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