Mobile app development

Cheat sheet · Level 2

Screens, saving and the server

Every command and idea of this level on one page: what to type, what it does, and what you see.

Take the level exam
  1. 1Go to a new screen

    A second screen, and a button that opens it.

    What to typeWhat it doesYou see
    <Stack.Screen name="Details" component={DetailsScreen} />A Details screen in the stack·
    <Button title="See the recipe" onPress={() => navigation.navigate("Details")} />The button opens it📱 City Eats
  2. 2Back, and the back button

    Leave a screen the way people expect.

    What to typeWhat it doesYou see
    function DetailsScreen({ navigation }) {Details gets navigation·
    <Button title="Back to today" onPress={() => navigation.goBack()} />The button goes back📱 City Eats
  3. 3Send data to the next screen

    One Details screen for every recipe.

    What to typeWhat it doesYou see
    <Pressable key={recipe.id} style={styles.row} onPress={() => navigation.navigate("Details", { recipe })}>Send the recipe·
    const { recipe } = route.params;Read it on the next screen·
    <Text>{recipe.emoji} {recipe.name} · {recipe.time} min</Text>Show its name·
    <Text>{recipe.time} minutes</Text>Show its time📱 🍅 Tomato soup · 25 min
  4. 4Tabs at the bottom

    Two main parts of the app, one tap apart.

    What to typeWhat it doesYou see
    <Tab.Screen name="Shopping" component={ShoppingScreen} options={{ tabBarIcon: () => <Text>🛒</Text> }} />A Shopping tab📱 Shopping
  5. 5A button in the header

    Put the shopping list in the top corner.

    What to typeWhat it doesYou see
    options={({ navigation }) => ({Options as a function·
    headerRight: () => <Button title="List" onPress={() => navigation.navigate("Shopping")} />,A button on the right📱 LIST
  6. 6Save on the phone

    Keep what the user typed, even after the app closes.

    What to typeWhat it doesYou see
    await AsyncStorage.setItem("name", name);Save under the key "name"·
    setSaved(true);Then show the tick📱 Your City Eats name
  7. 7Load saved data when the app opens

    Greet people by the name they saved.

    What to typeWhat it doesYou see
    AsyncStorage.getItem("name").then((saved) => {Load the key "name"·
    if (saved) setName(saved);Put it in state📱 Welcome back, Leo!
  8. 8A settings screen that remembers

    Save a whole object, not just one word.

    What to typeWhat it doesYou see
    AsyncStorage.getItem("settings").then((text) => {Load the settings·
    if (text) setSettings(JSON.parse(text));Text back into an object·
    AsyncStorage.setItem("settings", JSON.stringify(next));Save them as text📱 Showing vegetarian recipes
  9. 9Fetch a list from the server

    Today's recipes come from the kitchen's server.

    What to typeWhat it doesYou see
    fetch("/api/recipes")Ask the server·
    .then((res) => res.json())Read the answer as data·
    .then((data) => setRecipes(data));Put the list in stateGET /api/recipes · 200
  10. 10A loading spinner

    Never show an empty screen while you wait.

    What to typeWhat it doesYou see
    setLoading(false);Loading is over·
    if (loading) {Check loading first·
    <ActivityIndicator size="large" />A spinner📱 Loading recipes…
  11. 11Errors and Try again

    When the server has a bad moment, offer a way out.

    What to typeWhat it doesYou see
    .catch(() => setError("Couldn't load recipes."))Keep the error message·
    if (error) {Show it when there is one·
    <Button title="Try again" onPress={load} />Try again runs load📱 Couldn't load recipes.
  12. 12Pull to refresh

    Pull the list down to check for new recipes.

    What to typeWhat it doesYou see
    refreshing={refreshing}Is it refreshing?·
    onRefresh={refresh}What a pull runs📱 ↓ Pull to refresh
  13. 13Send a form to the server

    Share a recipe with the whole city.

    What to typeWhat it doesYou see
    method: "POST",Send with POST·
    headers: { "Content-Type": "application/json" },Say it's JSON·
    body: JSON.stringify({ name: name, time: Number(minutes) }),The body as text·
    await fetch("/api/recipes", {Wait for it📱 Share a recipe
  14. 14Check a form before sending

    Stop empty recipes before they reach the server.

    What to typeWhat it doesYou see
    if (!name.trim() || !Number(minutes)) {Check both boxes·
    setError("Add a name and the minutes.");Say what's missing📱 Share a recipe
  15. 15An offline banner

    No internet is not the server's fault: say so.

    What to typeWhat it doesYou see
    if (e instanceof TypeError) setOffline(true);No connection is a TypeError·
    else setError("The kitchen had a problem.");Otherwise, a server message📱 You're offline. Check your connection.
  16. 16On your own: the favorites tab

    Tabs, loading from the phone and clearing it.

    What to typeWhat it doesYou see
    const [favorites, setFavorites] = useState([]);Favorites in state·
    AsyncStorage.getItem("favorites").then((text) => {Load them when it opens·
    if (text) setFavorites(JSON.parse(text));Text back into a list·
    AsyncStorage.removeItem("favorites");Clear removes them from the phone📱 Favorites