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

[react] New useReducer signature #7420

Closed

Conversation

threepointone
Copy link

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.

@threepointone threepointone changed the title New usereducer signature New useReducer signature Jan 31, 2019
@threepointone threepointone changed the title New useReducer signature [react] New useReducer signature Jan 31, 2019
@threepointone
Copy link
Author

threepointone commented Jan 31, 2019

(updating tests, will update PR soon) EDIT: done.


const [state, dispatch] = React.useReducer(reducer, initialState, initialAction);
const [state, dispatch] = React.useReducer(reducer, { type: "reset", payload: 123 }, init);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super important, but why does the init arg in this example have a type and payload? Implies that it's the same type as the action, but that's not necessarily true.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason, just translated what was already there. I’ll change it. I specifically wanted it to not have the signature of the state, but I should have gone with something totally different. Thx!

@threepointone
Copy link
Author

I've added (as a separate commit) a bunch of .diff files that got generated. I"m honestly not sure whether that's expected.

@nmote nmote added the Library definitions Issues or pull requests about core library definitions label Feb 1, 2019
lib/react.js Outdated
init: void,
): [S, Dispatch<A>];

declare export function useReducer<S, I, A>(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best ordering of the generics for when you specify them explicitly or would <S, A, I> be better so that the action type line up in the same place? Is there any practical difference?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a practical difference, happy to align it. If I squint, I could make the argument that an initialArg would always 'occur' in source before any dispatch calls, so it's chronologically sound?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the TypeScript defs need S and A to line up in a separate type for some obscure reason and therefore adds I at the end. So switching it would line up with TS.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done and done

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@threepointone has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

declare export function useReducer<S, A>(
reducer: (S, A) => S,
initialState: S,
init: void,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this type overload different from the previous one?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise Flow will error on useReducer(reducer, state, undefined); because the argument count is different than what’s expected.

@bvaughn
Copy link
Contributor

bvaughn commented Feb 4, 2019

cc @jbrown215 This PR, #7430, and #7440 are all minor adjustments to things in the upcoming 16.8 release. Should we merge them into one PR to reduce the number of snapshot test conflicts?

@jbrown215
Copy link
Contributor

@bvaughn: That or stacking them will work! In either case, they'll need to be re-recorded.

Will review these tomorrow

@jbrown215 jbrown215 self-assigned this Feb 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Library definitions Issues or pull requests about core library definitions Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants