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.
1Go to a new screen
A second screen, and a button that opens it.
What to type What it does You 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 2Back, and the back button
Leave a screen the way people expect.
What to type What it does You see function DetailsScreen({ navigation }) {Details gets navigation · <Button title="Back to today" onPress={() => navigation.goBack()} />The button goes back 📱 City Eats 3Send data to the next screen
One Details screen for every recipe.
What to type What it does You 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 4Tabs at the bottom
Two main parts of the app, one tap apart.
What to type What it does You see <Tab.Screen name="Shopping" component={ShoppingScreen} options={{ tabBarIcon: () => <Text>🛒</Text> }} />A Shopping tab 📱 Shopping 5A button in the header
Put the shopping list in the top corner.
What to type What it does You see options={({ navigation }) => ({Options as a function · headerRight: () => <Button title="List" onPress={() => navigation.navigate("Shopping")} />,A button on the right 📱 LIST 6Save on the phone
Keep what the user typed, even after the app closes.
What to type What it does You see await AsyncStorage.setItem("name", name);Save under the key "name" · setSaved(true);Then show the tick 📱 Your City Eats name 7Load saved data when the app opens
Greet people by the name they saved.
What to type What it does You see AsyncStorage.getItem("name").then((saved) => {Load the key "name" · if (saved) setName(saved);Put it in state 📱 Welcome back, Leo! 8A settings screen that remembers
Save a whole object, not just one word.
What to type What it does You 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 9Fetch a list from the server
Today's recipes come from the kitchen's server.
What to type What it does You see fetch("/api/recipes")Ask the server · .then((res) => res.json())Read the answer as data · .then((data) => setRecipes(data));Put the list in state GET /api/recipes · 200 10A loading spinner
Never show an empty screen while you wait.
What to type What it does You see setLoading(false);Loading is over · if (loading) {Check loading first · <ActivityIndicator size="large" />A spinner 📱 Loading recipes… 11Errors and Try again
When the server has a bad moment, offer a way out.
What to type What it does You 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. 12Pull to refresh
Pull the list down to check for new recipes.
What to type What it does You see refreshing={refreshing}Is it refreshing? · onRefresh={refresh}What a pull runs 📱 ↓ Pull to refresh 13Send a form to the server
Share a recipe with the whole city.
What to type What it does You 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 14Check a form before sending
Stop empty recipes before they reach the server.
What to type What it does You see if (!name.trim() || !Number(minutes)) {Check both boxes · setError("Add a name and the minutes.");Say what's missing 📱 Share a recipe 15An offline banner
No internet is not the server's fault: say so.
What to type What it does You 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. 16On your own: the favorites tab
Tabs, loading from the phone and clearing it.
What to type What it does You 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