gpt4 book ai didi

reactjs - 无法在 React Native 应用程序中使用 React.memo

转载 作者:行者123 更新时间:2023-12-03 13:20:47 24 4
gpt4 key购买 nike

我想使用 React.memo 在 React Native 中进行优化。

import React from "react";
import { View, Text } from "react-native";

const Memo = React.memo(function() {
return (
<View>
<Text>Ok</Text>
</View>
);
});
class Demo extends React.Component {
render() {
return <Memo />;
}
}

export default Demo;

它给出以下错误:

TypeError : undefined is not a function (evaluating render(nextProps)). This error is located at : in Demo(at renderApplication.js:34) in RCTView(at View.js:44 )

我们可以在 React Native 中使用 React.memo 吗?

最佳答案

您尝试内存的“组件”不是正确的 react 组件,它没有传递任何 Prop 。 React memo HOC 也仅适用于功能组件,对于基于类的组件使用 PureComponent

您需要确保您使用的是引入了 memo HOC 的最新版本的 React,并按如下方式使用它:

import React, { Component, memo } from "react";
import { View, Text } from "react-native";

const Demo = props => (
<View>
<Text>Ok</Text>
</View>
);

export default memo(Demo); // memo HOC memoizes return component based on state and props!

React memo

关于reactjs - 无法在 React Native 应用程序中使用 React.memo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53351991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com