I have gone through a couple of articles on useCallback
and useMemo
on when to use and when not to use but I have mostly seen very contrived
code. I was looking at a code at my company where I have noticed someone have done this:
const takePhoto = useCallback(() => { launchCamera({ mediaType: "photo", cameraType: "front" }, onPickImage); }, []); const pickPhotoFromLibrary = async () => { launchImageLibrary({ mediaType: "photo" }, onPickImage); } const onUploadPress = useCallback(() => { Alert.alert("Upload Photo","From where would you like to take your photo?", [ { text: "Camera", onPress: () => takePhoto() }, { text: "Library", onPress: () => pickPhotoFromLibrary() }, ] ); }, [pickPhotoFromLibrary, takePhoto]);
This is how onUploadPress is called:
<TouchableOpacity style={styles.retakeButton} onPress={onUploadPress}>
Do you think this is the correct way of calling it? Based on my understanding from those articles, this looks in-correct. Can someone tell me when to use useCallback
and also maybe explain useCallback
in more human terms?
Article I read: When to useMemo and useCallback.