Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  157] [ 5]  / answers: 1 / hits: 9593  / 4 Years ago, sun, march 8, 2020, 12:00:00

I am working on an app and I am using bottomTabNavigator but in mean time I am getting this warning! ( 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.



I know I am doing something wrong but I didn't figure out what's wrong with my code. I am new to React native, could someone please help me how to solve this warning .



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

More From » reactjs

 Answers
1

Quick Solution


Move your component definition into a separate line of code


        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


Components use reference identity to determine when to re-renders ... so by passing component-definition as a prop, you're forcing it to create a new-object as a component with each-render ... causing unnecessary-re-renders for Tab.Screen, and no-state will be preserved between renders for YourComponent


[#4546] Wednesday, March 4, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yaquelina

Total Points: 517
Total Questions: 101
Total Answers: 96

Location: Egypt
Member since Tue, Jul 6, 2021
3 Years ago
;