我正在开发一个应用程序,我正在使用bottomTabNavigator,但与此同时,我收到了这个警告!( Look like you're passing an inline function for 'Component' prop for the screen 'Feed' (e.g component={()=><SomeComponent/>)). Passing an inline function will cause the component state to be lost on re-render and cause perf issue since it's re-created every render. You can pass the function as children to 'Screen' instead to achieve the desired behaviour.

我知道我做错了什么,但我不知道我的代码出了什么问题.我是新手,请有人帮我解决这个警告.

Code

 return (
      <NavigationContainer>
        <Tab.Navigator
          initialRouteName="Home"
          tabBarOptions={{
            activeTintColor: "#e91e63"
          }}
        >
          <Tab.Screen
            name="Home"
            component={props => (
              <PharmacyHome
                catId={this.props.navigation.state.params}
                {...props}
              />
            )}
            options={{
              tabBarLabel: "Home",
              tabBarIcon: ({ color, size }) => (
                <MaterialCommunityIcons name="home" color={color} size={size} />
              )
            }}
          />
          <Tab.Screen
            name="Notifications"
            component={Notifications}
            options={{
              tabBarLabel: "Updates",
              tabBarIcon: ({ color, size }) => (
                <MaterialCommunityIcons name="bell" color={color} size={size} />
              )
            }}
          />
          <Tab.Screen
            name="Profile"
            component={Profile}
            options={{
              tabBarLabel: "Profile",
              tabBarIcon: ({ color, size }) => (
                <MaterialCommunityIcons
                  name="account"
                  color={color}
                  size={size}
                />
              )
            }}
          />
        </Tab.Navigator>
      </NavigationContainer>
    );

推荐答案

Quick Solution

将组件定义移动到单独的代码行中

        component={props => (
          <PharmacyHome
            catId={this.props.navigation.state.params}
            {...props}
          />
        )}

Instead

const YourComponent = props => (
  <PharmacyHome catId={this.props.navigation.state.params} {...props} />
);

      <Tab.Screen
        name="Home"
        component={YourComponent}

Explanation

组件使用reference identity来确定何时重新渲染...因此,通过将组件定义作为props 传递,您将强制它在每次渲染时创建一个新对象作为组件...导致Tab.Screen次不必要的重新渲染,YourComponent次渲染之间不会保留任何状态

React-native相关问答推荐

expo 使用错误的版本代码创建建筑

未执行迁移?

在 react native 平面列表中动态定位弹出窗口

ReactNative:在所有组件和操作之间共享 sqlite 实例的最佳方法

react-native 自定义视图,原生 prop 没有 propType

Google SignIn SDK 因抛出错误而失败,发生不可恢复的登录失败 -catch 错误

在react-native 模块库中找不到 ios 框架头文件

React-native -run-ios 错误无法构建 iOS 元素.我们运行了xcodebuild命令,但它以错误代码 65 退出

react-native redux 和 ListView

React Native WebView 中的 Javascript console.log()

用于部署后设置的 React 配置文件

React Native - 如何判断 UI/元素?

React Native 无法读取 index.android.delta

可以在没有 await 关键字的情况下调用异步函数吗?

如何判断 goBack() 函数在react-navigation 中是否可行?

如何在react-native中设置 FlatList 的刷新指示器?

在 React Native App 中禁用屏幕捕获/ScreenShot

I18nManager.forceRTL 不会在首次应用加载时应用更改

如何在 React Native 中更改 textInput 占位符的字体系列

React Native 项目,有时使用 expo start 获得连接 ECONNREFUSED 127.0.0.1:19001