|
| 1 | +# Azure SDK samples for React Native using Expo (TypeScript) |
| 2 | + |
| 3 | +This sample application shows how to use the TypeScript client libraries for Azure in some common scenarios. |
| 4 | + |
| 5 | +In this sample, we build a simple application in React Native using Expo and integrating with Azure App Configuration service. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +The samples are compatible with [LTS versions of Node.js](https://nodejs.org/about/releases/). |
| 10 | + |
| 11 | +Before running the samples in Node, they must be compiled to JavaScript using the TypeScript compiler. For more information on TypeScript, see the [TypeScript documentation][typescript]. |
| 12 | + |
| 13 | +The sample is using [Expo](https://expo.dev/), a framework and platform for universal React applications. |
| 14 | + |
| 15 | +You need [an Azure subscription][freesub] and the following resources created to run this sample: |
| 16 | + |
| 17 | +## Step by step |
| 18 | + |
| 19 | +### Create the project |
| 20 | + |
| 21 | +Run expo CLI to create a blank project with typescript support, give it a name of `appconf-rn-expo`. |
| 22 | + |
| 23 | +```shell |
| 24 | +expo init -t expo-template-blank-typescript |
| 25 | +``` |
| 26 | + |
| 27 | +At this point, we should be able to see the app running if the environment is set up correctly |
| 28 | + |
| 29 | +```shell |
| 30 | +expo start --android |
| 31 | +``` |
| 32 | + |
| 33 | +### Add a button to the app |
| 34 | + |
| 35 | +Open App.tsx and add the following code so it looks similar to following code |
| 36 | +```diff |
| 37 | +-import { StyleSheet, Text, View } from 'react-native'; |
| 38 | ++import { Button, StyleSheet, Text, View } from 'react-native'; |
| 39 | ++import { runAppConfigSample } from "./appConfigSample"; |
| 40 | +... |
| 41 | + |
| 42 | + <Text>Open up App.tsx to start working on your app!</Text> |
| 43 | ++ <Button title="Click me!" onPress={() => runAppConfigSample() } /> |
| 44 | + <StatusBar style="auto" /> |
| 45 | +``` |
| 46 | + |
| 47 | +### Add the App Configuration sample file |
| 48 | + |
| 49 | +create a new file `appConfigSample.ts` in the sample directory as `App.tsx` with following content from [appConfigSample.ts](./appConfigSample.ts) file |
| 50 | + |
| 51 | +### Add dependencies |
| 52 | + |
| 53 | +We need to add a dependency to the JavaScript Client SDK library for Azure App Configuration (version 1.4.0-beta.1 or later). We use `@babel/plugin-proposal-async-generator-functions` to help transform async iterator usage in our sample. We also use `babel-plugin-inline-dotenv` to help load secrets from our `.env` file while developing. |
| 54 | + |
| 55 | +```shell |
| 56 | + |
| 57 | +yarn add --dev babel-plugin-inline-dotenv @babel/plugin-proposal-async-generator-functions |
| 58 | +``` |
| 59 | + |
| 60 | +Then add the following into `babel.config.js` to enable the plugin |
| 61 | + |
| 62 | +```diff |
| 63 | + presets: ["babel-preset-expo"], |
| 64 | ++ plugins: [ |
| 65 | ++ "@babel/plugin-proposal-async-generator-functions", |
| 66 | ++ ["inline-dotenv", { unsafe: true }], |
| 67 | ++ ], |
| 68 | +``` |
| 69 | + |
| 70 | +### Add connection string to .env file |
| 71 | + |
| 72 | +Create a `.env` file in the project directory. Retrieve your connection string from Azure portal and add them to the `.env` file. Since this file contains secrets you need to add it to the ignore list if your code is committed to a repository. |
| 73 | + |
| 74 | +**Note** We use connection string directly here for testing purpose. You should consider the trade-off between security and convenience and better use a backend to dynamically provide secrets to only authenticated users. |
| 75 | + |
| 76 | +``` |
| 77 | +APPCONFIG_CONNECTION_STRING="<your app configuration connection string>" |
| 78 | +``` |
| 79 | + |
| 80 | +**Note**: if you update the .env file, you would need to clear expo cache and rebuild. It can be done by passing `-c` to the start command, for example, in `package.json` |
| 81 | + |
| 82 | +```diff |
| 83 | +- "android": "expo start --android", |
| 84 | ++ "android": "expo start --android -c", |
| 85 | +``` |
| 86 | + |
| 87 | +### Add polyfills |
| 88 | + |
| 89 | +The `crypto` dependency of the JavaScript Client SDK library for Azure App Configuration are not available in React-Native, so we need to provide polyfills for them. We also need to polyfill the `TextEncoder` API. Add the following dependencies to the project |
| 90 | + |
| 91 | +```shell |
| 92 | +yarn add isomorphic-webcrypto react-native-get-random-values react-native-url-polyfill text-encoding-polyfill |
| 93 | +``` |
| 94 | + |
| 95 | +Then add a `shims.ts` file in the project root with the following content |
| 96 | + |
| 97 | +```typescript |
| 98 | +import 'react-native-url-polyfill/auto'; |
| 99 | +import "react-native-get-random-values"; |
| 100 | +import "text-encoding-polyfill"; |
| 101 | +const getRandomValues = global.crypto.getRandomValues; |
| 102 | +import * as crypto from "isomorphic-webcrypto"; |
| 103 | +global.crypto = crypto; |
| 104 | +global.crypto.getRandomValues = getRandomValues; |
| 105 | +``` |
| 106 | + |
| 107 | +Then add the following line to `App.tsx` |
| 108 | + |
| 109 | +```diff |
| 110 | ++import "./shims"; |
| 111 | +import { runAppConfigSample } from "./appConfigSample"; |
| 112 | +``` |
| 113 | + |
| 114 | +Now run the app in the emulator (Android in this case) |
| 115 | + |
| 116 | +```shell |
| 117 | +npm run android |
| 118 | +``` |
| 119 | + |
| 120 | +Click on the **CLICK ME!** button, you should see the following logged to the console output! |
| 121 | + |
| 122 | +``` |
| 123 | +Running helloworld sample |
| 124 | +Adding in new setting Samples:Greeting |
| 125 | +Samples:Greeting has been set to Hello! |
| 126 | +Samples:Greeting has been set to Goodbye! |
| 127 | +Samples:Greeting has been deleted |
| 128 | +``` |
0 commit comments