-
Notifications
You must be signed in to change notification settings - Fork 3
PhotosScreen: Show an Alert
Starting point: https://github.com/andrekovac/react-native-workshop/tree/09-react-fetch-and-display-images-with-async-await
In fact: Clone the PhotoFavorite repository and checkout the 01-c-fetch-images-async-await
branch via git switch 01-c-fetch-images-async-await
Show an Alert to the user when an image in the list is tapped. The Alert contains the title Artist and displays the property author
of the image JSON response.
-
Inside the Item component, wrap the
Image
in aTouchableOpacity
. -
In the
onPress
prop of theTouchableOpacity
implement a callback function which opens an Alert.Use the following code for an Alert:
Alert.alert("Artist", author, [{ text: "OK" }], { cancelable: false });
Don't forget to import the Alert
Component.
- Pass the
author
prop from thePhotoList
component to theItem
component.
- Take a look at the following diff: https://github.com/andrekovac/PhotoFavorite/commit/d2e37ef989f14dde181adc43472885aaaea5dee6
- It adds a commit ot the solution where a
handlePress
function is defined. Observe how it is passed to theonPress
prop. - Why would it not work to add
handlePress
toonPress
asonPress={handlePress}
. What would the first argument of thehandlePress
callback implicitly be?
Solution file: https://github.com/andrekovac/PhotoFavorite/blob/01-d-show-alert-on-press/src/screens/PhotosScreen.tsx
Solution diff: https://github.com/andrekovac/PhotoFavorite/commit/ab60427e946a5ec45767ae19caa71e71cf3e9b7c
Solution branch: https://github.com/andrekovac/PhotoFavorite/tree/01-d-show-alert-on-press