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

Change class components to functional components #5431

Merged
merged 4 commits into from
Oct 26, 2022

Conversation

nderkim
Copy link
Collaborator

@nderkim nderkim commented Oct 26, 2022

No description provided.

@changeset-bot
Copy link

changeset-bot bot commented Oct 26, 2022

🦋 Changeset detected

Latest commit: 4dcb007

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
react-select Patch
@react-select/docs Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 26, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 4dcb007:

Sandbox Source
react-codesandboxer-example Configuration

@nderkim nderkim force-pushed the alex/refactor-class-components-to-functional branch from 29198dc to 0ca2d5b Compare October 26, 2022 04:13
const getPortalPlacement = useCallback(
({ placement: updatedPlacement }: CalculatedMenuPlacementAndHeight) => {
// avoid re-renders if the placement has not changed
if (updatedPlacement !== placement) {
Copy link
Collaborator 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 we need this check because React knows to bail out if the value did not change.

exited: { width: 0 },
};
nodeRef = createRef<HTMLDivElement>();
export const Collapse = ({ children, in: _in, onExited }: CollapseProps) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume that in is from "react-transition-group"—reserved word for a prop was such a bad idea...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

agreed 🥲

{(state) => (
<div
ref={ref}
style={{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Could the nested ternaries be against an object with state keys, or some getStyleFromState fn with a switch?

@@ -55,10 +55,10 @@ interface PlacementArgs {
}

export function getMenuPlacement({
maxHeight,
maxHeight: desiredMaxHeight,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Glad these are now differentiated! I think "desired" isn't quite right, though. Maybe preferredX or consumerX?

placement: CoercedMenuPlacement | null;
maxHeight: number;
}
const PortalPlacementContext =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Declaring undefined feels weird. Could this be:

createContext<CtxType | undefined>()

// OR

createContext<CtxType | null>(null)

const [placement, setPlacement] = useState<CoercedMenuPlacement | null>(null);

useLayoutEffect(() => {
const { current } = ref;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: destructuring doesn't offer a lot of value here, prefer meaningful variable name e.g.

useLayoutEffect(() => {
  const menuEl = ref.current
  if (!menuEl) return

  const state = getMenuPlacement({
    menuEl,
    ...
  })
})

}
}
return (
<Fragment>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I get why this is here, but I think it's a mistake in our types 🤔 For another day:

export interface MenuPlacerProps {
-  children: (childrenProps: ChildrenProps) => ReactNode
+  children: (childrenProps: ChildrenProps) => ReactElement
}

@@ -690,7 +684,7 @@ export const MenuPortal = <
);

return (
<PortalPlacementContext.Provider value={{ getPortalPlacement }}>
<PortalPlacementContext.Provider value={{ setPortalPlacement }}>
Copy link
Collaborator

Choose a reason for hiding this comment

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

State setters are equal between renders, but the object will be created each time. Might be worth memoizing to reduce consumer re-renders?

const context = useMemo(() => ({ setPortalPlacement }), [setPortalPlacement])

return <PortalPlacementContext.Provider value={context} />

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

nice catch! 🎉

Copy link
Collaborator

@jossmac jossmac left a comment

Choose a reason for hiding this comment

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

Couple of nit comments, but LGTM 🚀

@nderkim nderkim merged commit 7df4094 into master Oct 26, 2022
@nderkim nderkim deleted the alex/refactor-class-components-to-functional branch October 26, 2022 05:23
@github-actions github-actions bot mentioned this pull request Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants