为什么文档使用useCallback和useMemo来记忆为BottomSheet传递的道具,性能如何呢?
https://gorhom.github.io/react-native-bottom-sheet/usage
import React, { useCallback, useMemo, useRef } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import BottomSheet from '@gorhom/bottom-sheet';
const App = () => {
// ref
const bottomSheetRef = useRef<BottomSheet>(null);
// variables
const snapPoints = useMemo(() => ['25%', '50%'], []);
// callbacks
const handleSheetChanges = useCallback((index: number) => {
console.log('handleSheetChanges', index);
}, []);
// renders
return (
<View style={styles.container}>
<BottomSheet
ref={bottomSheetRef}
index={1}
snapPoints={snapPoints}
onChange={handleSheetChanges}
>
<View style={styles.contentContainer}>
<Text>Awesome </Text>
</View>
</BottomSheet>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 24,
backgroundColor: 'grey',
},
contentContainer: {
flex: 1,
alignItems: 'center',
},
});
export default App;发布于 2022-11-01 14:28:41
我相信这是一个不必要的优化。至少在这个例子中。
回忆录没有提高性能,因为在这些功能中没有大量的计算。
但是,当需要优化计算快照或更改事件处理程序时,作者可能会使用它们作为可能的占位符。
这是一个很好的解释,什么时候需要这些钩子。
https://stackoverflow.com/questions/71432055
复制相似问题