Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

When to use useCallback in React?

$
0
0

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: https://kentcdodds.com/blog/usememo-and-usecallback.


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>