react-navigation icon indicating copy to clipboard operation
react-navigation copied to clipboard

Can't use hooks in DrawerContent

Open trungtranmobile97 opened this issue 6 years ago • 9 comments

Current Behavior

  • When I use the hook in the drawer content screens, I get an error even though I am using the hook.
  • Screen Drawer.tsx `import React from 'react'; import {createDrawerNavigator} from '@react-navigation/drawer'; import DrawerContent from './DrawerContent'; import BottomTab from '../BottomTab/BottomTab';

const DrawerNavigator = createDrawerNavigator();

const Drawer = () => { return ( <DrawerNavigator.Navigator initialRouteName="Drawer" drawerContent={DrawerContent}> <DrawerNavigator.Screen name="BottomTab" component={BottomTab} /> </DrawerNavigator.Navigator> ); };

export default Drawer; `

  • DrawerContent.tsx `import React from 'react'; import {View} from 'react-native'; import {useState} from 'react'; import {Button} from 'Components'; import {Strings} from 'Localization';

const DrawerContent = () => { const [name, setName] = useState('Your name'); const logout = () => {}; return ( <View style={{flex: 1, justifyContent: 'center'}}>

Hey! Thanks for opening the issue. Can you provide a minimal repro which demonstrates the issue? Posting a snippet of your code in the issue is useful, but it's not usually straightforward to run. A repro will help us debug the issue faster. Please try to keep the repro as small as possible. The easiest way to provide a repro is on snack.expo.io. If it's not possible to repro it on snack.expo.io, then you can also provide the repro in a GitHub repository.

github-actions[bot] avatar Mar 04 '20 08:03 github-actions[bot]

Drawer content accepts render function so the function you're providing is not considered as a component. If you want to achieve this you can do:

drawerContent={props => <DrawerContent {...props} />}

where DrawerContent can be any component you want :)

osdnk avatar Mar 06 '20 14:03 osdnk

Drawer content accepts render function so the function you're providing is not considered as a component. If you want to achieve this you can do:

drawerContent={props => <DrawerContent {...props} />}

where DrawerContent can be any component you want :)

I really love you man! You save my day! LOL

wellmmer avatar Apr 06 '20 21:04 wellmmer

Can I connect DrawerContent component with my redux store? I want to connect DrawerContent with my redux store to update redux state. plz, guide me on it.

shehrazjazib avatar Jun 23 '20 18:06 shehrazjazib

@shehrazjazib You can. Just useSelector and useDispatch from react-redux will help you with this.

cseelus avatar Jun 27 '20 15:06 cseelus

@cseelus thanks. I implement drawer in main app compenot which is connected with redux store. 2nd to customize my drawer, i setup drawercontent in another react component, which is also connected with redux store In this way i have achieved it.

shehrazjazib avatar Jun 27 '20 18:06 shehrazjazib

{...props} />

Great tip dude. Thumbs up saved my day <3

mohdaamir8182 avatar Mar 02 '21 12:03 mohdaamir8182

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.

github-actions[bot] avatar Feb 04 '22 22:02 github-actions[bot]

i was using it like drawerContent={DrawerContent}

changed it to drawerContent={props => <DrawerContent {...props} />}

now working.

nibinb avatar Jul 07 '23 08:07 nibinb