react-native useMemo
时间: 2023-12-05 17:29:40 浏览: 80
React Native's `useMemo` hook is a hook that allows you to memoize the value of a function or an object, preventing unnecessary re-computation of that value on subsequent renders.
Here's an example of how you can use `useMemo` in React Native:
```javascript
import React, { useMemo } from 'react';
import { View, Text } from 'react-native';
const MyComponent = ({ data }) => {
const processedData = useMemo(() => {
// Expensive computation or transformation logic here
return data.map(item => item * 2);
}, [data]);
return (
<View>
<Text>{processedData}</Text>
</View>
);
};
export default MyComponent;
```
In the above example, the `processedData` value will only be re-computed if the `data` prop changes. If the `data` prop remains the same between renders, the memoized value will be returned from a cache, avoiding unnecessary computations.
By using `useMemo`, you can optimize your component's performance by preventing unnecessary re-renders when the dependencies haven't changed.
阅读全文