React Native 警告:包含“key”prop 的 props 对象正在通过 BottomTab.Navigator 和react-native-paper 传播到 JSX 中

问题描述 投票:0回答:1

当我使用

BottomTab.Navigator
中的
@react-navigation/bottom-tabs
BottomNavigation.Bar
 中的 
react-native-paper

时,我在 React Native 项目中遇到以下警告

错误

Warning: A props object containing a "key" prop is being spread into JSX: 
<Touchable {...props} />
React keys must be passed directly to JSX without using spread.

这是我如何设置底部选项卡导航的代码片段:

function AppNav({ routes }) {
  const renderTabBar = ({ navigation, state, descriptors, insets }) => {
    const navigationBarProps = {
      version: 3,
      style: {
        backgroundColor: "#212529",
      },
      activeColor: "orange",
      inactiveColor: "lightgrey",
      shifting: false,
      navigationState: state,
      safeAreaInsets: insets,
      onTabPress: ({ route, preventDefault }) => {
        const event = navigation.emit({
          type: "tabPress",
          target: route.key,
          canPreventDefault: true,
        });

        if (event.defaultPrevented) {
          preventDefault();
        } else {
          navigation.dispatch({
            ...CommonActions.navigate(route.name, route.params),
            target: state.key,
          });
        }
      },
      renderIcon: ({ route, focused }) => {
        const { options } = descriptors[route.key];
        return options.tabBarIcon ? options.tabBarIcon({ focused, size: 24 }) : null;
      },
      getLabelText: ({ route }) => {
        const { options } = descriptors[route.key];
        return options.tabBarLabel ?? route.name;
      },
    };

    return <BottomNavigation.Bar {...navigationBarProps} />;
  };

  return (
    <BottomTab.Navigator
      screenOptions={{ headerShown: false }}
      tabBar={renderTabBar}
    >
      {routes?.map((route) => (
        <BottomTab.Screen
          key={route.key}
          name={route.name}
          component={route.component}
          options={{
            title: route.name,
            tabBarIcon: ({ size, focused }) => (
              <MaterialCommunityIcons
                name={focused ? route.icon_focused : route.icon_default}
                color={focused ? "orange" : "#fafafa"}
                size={size}
              />
            ),
          }}
        />
      ))}
    </BottomTab.Navigator>
  );
}

当我升级依赖项时发生这种情况。这是我当前的软件包版本:

    "react": "18.3.1",
    "react-native": "0.76.1",
    "expo": "^52.0.4",
    "@react-navigation/bottom-tabs": "^6.5.19",
    "@react-navigation/drawer": "^6.6.14",
    "@react-navigation/native": "^6.1.16",
    "@react-navigation/native-stack": "^6.9.25",

javascript react-native expo new-architecture
1个回答
0
投票

是的,这是由于 React 18 中的更改所致。

您现在必须将

key
作为单独的参数传递。

示例

const { key, ...rest } = props;

<SomeComponent key={key} {...rest} />
© www.soinside.com 2019 - 2024. All rights reserved.