Performance Optimization
FlatList optimization, memo, useMemo/useCallback, Hermes, profiling, bundle size
1What is the main role of React.memo in React Native performance optimization?
What is the main role of React.memo in React Native performance optimization?
回答
React.memo is a Higher-Order Component that memoizes the render result of a functional component. It prevents unnecessary re-renders by comparing current props with previous ones (shallow comparison by default). If props haven't changed, React reuses the memoized result instead of re-rendering the component, which significantly improves performance in lists or complex component trees.
2Which FlatList property significantly optimizes scrolling by providing item dimensions in advance?
Which FlatList property significantly optimizes scrolling by providing item dimensions in advance?
回答
getItemLayout is an optional FlatList property that allows pre-calculating the height, width, and offset of each item. This prevents React Native from dynamically measuring each item, which considerably improves scroll performance, especially when jumping to a specific index with scrollToIndex. This optimization is particularly useful when all items have the same size.
3What is the main difference between useMemo and useCallback in terms of optimization?
What is the main difference between useMemo and useCallback in terms of optimization?
回答
useMemo memoizes the result of an expensive calculation and returns that computed value, while useCallback memoizes a function itself and returns that function. useMemo is useful for avoiding heavy recalculations on each render, whereas useCallback is essential for avoiding recreating functions passed as props to memoized child components, which would break their React.memo optimization.
What is Hermes and what is its main advantage for React Native applications?
Why is it crucial to provide a stable keyExtractor function to FlatList?
+21 面接問題