gpt4 book ai didi

javascript - 如何删除 React Native 中的警告

转载 作者:行者123 更新时间:2023-11-28 16:45:45 24 4
gpt4 key购买 nike

我正在开发一个应用程序,并且正在使用 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.

我知道我做错了什么,但我不知道我的代码出了什么问题。我是 React Native 的新手,有人可以帮我解决这个警告吗?

代码

 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>
);

最佳答案

快速解决方案

将组件定义移至单独的代码行

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

相反

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

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

说明

组件使用引用标识来确定何时重新渲染......因此,通过将组件定义作为 Prop 传递,您将强制它创建一个新对象作为每个组件的组件-render ...导致 Tab.Screen 不必要的重新渲染,并且在 YourComponent

的渲染之间将保留无状态

关于javascript - 如何删除 React Native 中的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60586470/

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