Skip to content

Commit

Permalink
fix: Authenticator issue where InitMachine useEffect runs every render (
Browse files Browse the repository at this point in the history
  • Loading branch information
reesscot authored Mar 8, 2022
1 parent 382c8b0 commit 8ead973
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-camels-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aws-amplify/ui-react": patch
---

fix: Authenticator issue where InitMachine useEffect runs every render, causing `children` of `Authenticator` to be unmounted and remounted on every render.
32 changes: 18 additions & 14 deletions packages/react/src/components/Authenticator/Authenticator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ export type AuthenticatorProps = AuthenticatorMachineOptions &
RouterProps &
ComponentsProviderProps;

// Helper component that sends init event to the parent provider
function InitMachine({ children, ...data }) {
const { _send, route } = useAuthenticator();

const hasInitialized = React.useRef(false);

React.useEffect(() => {
if (!hasInitialized.current && route === 'idle') {
_send({
type: 'INIT',
data,
});
hasInitialized.current = true;
}
}, [_send, route, data]);
return <>{children}</>;
}

export function Authenticator({
children,
className,
Expand All @@ -43,20 +61,6 @@ export function Authenticator({
formFields,
};

// Helper component that sends init event to the parent provider
function InitMachine({ children, ...machineProps }) {
const { _send, route } = useAuthenticator();
React.useEffect(() => {
if (route === 'idle') {
_send({
type: 'INIT',
data: machineProps,
});
}
}, []);
return <>{children}</>;
}

return (
<Provider>
<CustomComponentsContext.Provider value={{ components }}>
Expand Down

0 comments on commit 8ead973

Please sign in to comment.