- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Custom contentComponent in Drawer re-render everytime #6718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What is the problem with re rendering? |
I'm using PureComponent, but still its keep re rendering
Yes, and this problem is in react-navigation@4.x , I wanted to move away from that problem that's why I was trying react-navigation@5.x api |
Also as |
The premise of React is that you let React optimize updates for you so you don't have to think about re-renders. Is it actually causing any slow down in your app? If it is causing a slow down and your custom component is heavy, you can optimize your component with
Because navigation state is nested. Changing child state changes state of parent. |
Bro I have used this is the same issue as |
That's a completely different report regarding unmounting on navigating away.
You can't just put You'll need to extract expensive parts out to separate component, pass the minimum required data as props to it, and then apply |
As I mentioned, the child navigation state changes which causes parent state to change. Re-rendering the component is expected behaviour and not a bug. If the view is simple, why do you care about re-rendering? If there's nothing expensive then you don't need to prevent re-render. Is it actually causing performance issues for you? |
Bro this is my actual screen,(I'm testing on dummy that I showed you above) now the custom drawer component keeps re-rendering even if I don't click anything, and no state or no async operation is happening, nothing, and the drawer component re renders in react-navigation@4.x which is making my app slow to prevent that i'm trying react navigation@5.x so that once drawer component loads it doesn't re-render on its own, although the drawer component doesn't re-render on its own, but it is re-rendering whenever I am changing screen |
Please atleast once, please just clone the repo, change it to branch |
If your drawer is expensive to re-render, extract the expensive parts to separate component and apply pure component. class MyCustomComponent extends PureComponent {
// ... your expensive component
}
const ContentComponent = ({ navigation }) => {
// Don't pass all the props so we don't trigger re-render
// Only pass required props
return <MyCustomComponent navigation={navigation} />
}
// ...
<Drawer.Navigator contentComponent={ContentComponent} /> React Navigation cannot optimize your component. Only you know what props can safely trigger a re-render and what props cannot in your custom component. |
Dude, I already told you its expected behaviour. I won't find something new by running your code. I already know that the custom component will re-render. If re-render is expensive in your case, you need optimize your custom component manually. React Navigation cannot prevent re-render automatically for the reason I mentioned. |
bro this is my custom component, which keeps re-rendering, could you tell me which part should I extract it out const DrawerPage = () => {
console.log('Rendering Drawer Navigation!');
return (
<View>
<Text>DrawerPage</Text>
<Button
title="Close Drawer"
onPress={() => this.props.navigation.toggleDrawer()}
/>
</View>
);
} |
See my code https://github.com/react-navigation/navigation-ex/issues/99#issuecomment-531475410 Put your custom component and |
I did that, now only Thank you so much for your help and your patience |
I was also facing the same issue, here is how I managed to solve mine. logic: check if the drawer is open or close, return without doing heavy stuff if the drawer is closed.
|
I see you are using react-navigation 5, the umountOnBlur option worked great for me: https://stackoverflow.com/a/62675176/5970652 |
Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro. |
Current Behavior
I have implemented a Drawer which host a Stack navigator inside. I am giving a custom component to be render in
contentComponent
in Drawer. But the custom component in Drawer keeps rendering after every:Expected Behavior
How to reproduce
git clone https://github.com/alexakasanjeev/magento_react_native.git && cd magento_react_native
git checkout feature/react-navigation
npm install
npm start
react-native run-android
Debug Js Remotely
from dev menuScreenshots
Your Environment
The text was updated successfully, but these errors were encountered: