首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么snapPoints记忆与useMemo反应-本机底页

为什么snapPoints记忆与useMemo反应-本机底页
EN

Stack Overflow用户
提问于 2022-03-10 23:40:15
回答 1查看 450关注 0票数 1

为什么文档使用useCallbackuseMemo来记忆为BottomSheet传递的道具,性能如何呢?

https://gorhom.github.io/react-native-bottom-sheet/usage

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-01 14:28:41

我相信这是一个不必要的优化。至少在这个例子中。

回忆录没有提高性能,因为在这些功能中没有大量的计算。

但是,当需要优化计算快照或更改事件处理程序时,作者可能会使用它们作为可能的占位符。

这是一个很好的解释,什么时候需要这些钩子。

https://www.joshwcomeau.com/react/usememo-and-usecallback/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71432055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档