close
Skip to content

Improve Reducer Hook's lazy init API - #14723

Merged
acdlite merged 2 commits into
react:masterfrom
acdlite:reducerhooklazyinit
Jan 30, 2019
Merged

Improve Reducer Hook's lazy init API#14723
acdlite merged 2 commits into
react:masterfrom
acdlite:reducerhooklazyinit

Conversation

@acdlite

@acdlite acdlite commented Jan 30, 2019

Copy link
Copy Markdown
Collaborator

No description provided.

@gaearon

gaearon commented Jan 30, 2019

Copy link
Copy Markdown
Collaborator

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

acdlite commented Jan 30, 2019

Copy link
Copy Markdown
Collaborator Author
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
Copy Markdown
Contributor

@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

acdlite commented Jan 30, 2019

Copy link
Copy Markdown
Collaborator Author

@sebmarkbage Good call, I edited my comment accordingly

@acdlite
acdlite merged commit ba6477a into react:master Jan 30, 2019
@bvaughn

bvaughn commented Jan 30, 2019

Copy link
Copy Markdown
Contributor
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

gaearon commented Jan 30, 2019

Copy link
Copy Markdown
Collaborator

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

bvaughn commented Jan 30, 2019

Copy link
Copy Markdown
Contributor

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

@Jessidhia

Copy link
Copy Markdown
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 react/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.
mrizwanashiq pushed a commit to mrizwanashiq/react that referenced this pull request Jun 25, 2026
* 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