Skip to content

Commit 8efcad1

Browse files
authored
update react quickstart to revise todo state, authenticator placement (#8016)
1 parent 58be951 commit 8efcad1

File tree

1 file changed

+49
-22
lines changed
  • src/pages/[platform]/start/quickstart

1 file changed

+49
-22
lines changed

src/pages/[platform]/start/quickstart/index.mdx

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -436,36 +436,63 @@ This should start a local dev server at http://localhost:5173.
436436

437437
The starter application already has a pre-configured auth backend defined in the **amplify/auth/resource.ts** file. We've configured it to support email and password login but you can extend it to support a variety of login mechanisms, including Google, Amazon, Sign In With Apple, and Facebook.
438438

439-
The fastest way to get your login experience up and running is to use our Authenticator UI component. In your **src/App.tsx** file, import the Authenticator UI component and wrap your `<main>` element.
439+
The fastest way to get your login experience up and running is to use our Authenticator UI component. In your **src/main.tsx** file, import the Authenticator UI component and wrap your `<App>` component.
440+
441+
```tsx title="src/main.tsx"
442+
import React from 'react';
443+
import ReactDOM from 'react-dom/client';
444+
// highlight-next-line
445+
import { Authenticator } from '@aws-amplify/ui-react';
446+
import { Amplify } from 'aws-amplify';
447+
import App from './App.tsx';
448+
import outputs from '../amplify_outputs.json';
449+
import './index.css';
450+
// highlight-next-line
451+
import '@aws-amplify/ui-react/styles.css';
452+
453+
Amplify.configure(outputs);
454+
455+
ReactDOM.createRoot(document.getElementById('root')!).render(
456+
// highlight-start
457+
<React.StrictMode>
458+
<Authenticator>
459+
<App />
460+
</Authenticator>
461+
</React.StrictMode>
462+
// highlight-end
463+
);
464+
```
465+
466+
The Authenticator component auto-detects your auth backend settings and renders the correct UI state based on the auth backend's authentication flow.
467+
468+
In your `src/App.tsx` file, add a button to enable users to sign out of the application. Import the [`useAuthenticator`](https://ui.docs.amplify.aws/react/connected-components/authenticator/advanced#access-auth-state) hook from the Amplify UI library to hook into the state of the Authenticator.
440469

441470
```tsx title="src/App.tsx"
442-
// highlight-start
443-
import { Authenticator } from '@aws-amplify/ui-react'
444-
import '@aws-amplify/ui-react/styles.css'
445-
// highlight-end
446-
// ... other imports
471+
import type { Schema } from '../amplify/data/resource';
472+
// highlight-next-line
473+
import { useAuthenticator } from '@aws-amplify/ui-react';
474+
import { useEffect, useState } from 'react';
475+
import { generateClient } from 'aws-amplify/data';
476+
477+
const client = generateClient<Schema>();
447478

448479
function App() {
480+
// highlight-next-line
481+
const { signOut } = useAuthenticator();
482+
449483
// ...
484+
450485
return (
451-
// highlight-start
452-
<Authenticator>
453-
{({ signOut }) => (
454-
// highlight-end
455-
<main>
456-
{/*...*/}
457-
// highlight-next-line
458-
<button onClick={signOut}>Sign out</button>
459-
</main>
460-
// highlight-start
461-
)}
462-
</Authenticator>
463-
// highlight-end
464-
)
486+
<main>
487+
{/* ... */}
488+
// highlight-next-line
489+
<button onClick={signOut}>Sign out</button>
490+
</main>
491+
);
465492
}
466-
```
467493

468-
The Authenticator component auto-detects your auth backend settings and renders the correct UI state based on the auth backend's authentication flow.
494+
export default App;
495+
```
469496

470497
Try out your application in your localhost environment again. You should be presented with a login experience now.
471498

0 commit comments

Comments
 (0)