iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙆

Setting Up Amplify Gen2 with React Native (Expo)

に公開

Conclusion (Important Notes)

  • Expo Go is no longer supported starting from Amplify Gen2
  • You need to run npx expo prebuild
  • Execute locally with npx expo run
    • Amplify does not work when using npx expo start
      • Cognito encountered an authentication error and could not proceed

This article is a summary of the official documentation organized in a way that is easy for me to understand.
https://docs.amplify.aws/react-native/start/quickstart/#create-backend

Environment

  • Amplify Gen2
  • Apple M2 Sonoma ver 14.3.1 (23D60)

Create

npx create-expo-app my_amplify_app -t expo-template-blank-typescript
cd my_amplify_app

npx expo prebuild
npm create amplify@latest

npx ampx sandbox

npm install

npm add \
  @aws-amplify/ui-react-native \
  @aws-amplify/react-native \
  aws-amplify \
  @react-native-community/netinfo \
  @react-native-async-storage/async-storage \
  react-native-safe-area-context \
  react-native-get-random-values \
  react-native-url-polyfill
npx pod-install

Execution

npx expo run


It takes more than 5 minutes, so you might want to listen to a song or two while waiting.

Note

npx expo start

If you use npx expo start, an "An unknown error has occurred" error appears on the Amplify Cognito screen, so make sure to use npx expo run instead.

Supplement

By the way, npx expo start launches the app in Expo Go (which does not support Amplify Gen2), whereas npx expo run performs a native install?, allowing you to use Amplify normally. I've included images below to help you identify whether the app is currently running in Expo Go.

Screen when running npx expo run (native install?)


Screen when running npx expo start (Execution using Expo Go)


With that, we've reached the point where React Native + Amplify Gen2 is running locally.

Reference

https://docs.amplify.aws/react-native/start/quickstart/#create-backend

Postscript

"The one where the startup command is actually in the official Docs"
"I wish they'd just written the startup command in the Docs..."
"Now no one has to struggle with this error anymore"

2024/05/30

Discussion