Can't use hooks in DrawerContent
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'}}> ); };
export default DrawerContent;
-Navigation.tsx
import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import Drawer from './Drawer/Drawer';
const Stack = createStackNavigator();
const Navigation = () => {
return (
export default Navigation;
-App.tsx
import 'react-native-gesture-handler';
import React from 'react';
import {Provider} from 'react-redux';
import {Store} from 'ReduxManager';
import Navigation from 'Navigation';
const App = () => {
return (
export default App;`
- Error: Unhandled JS Exception: Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
This error is located at: in DrawerView (at DrawerView.tsx:206) in SafeAreaProviderCompat (at DrawerView.tsx:204) in RCTView (at DrawerView.tsx:203) in DrawerView (at createDrawerNavigator.tsx:42) in DrawerNavigator (at Drawer.tsx:10) in Drawer (at SceneView.tsx:98) in StaticContainer in StaticContainer (at SceneView.tsx:89) in EnsureSingleNavigator (at SceneView.tsx:88) in SceneView (at useDescriptors.tsx:125) in RCTView (at CardContainer.tsx:187) in RCTView (at CardContainer.tsx:186) in RCTView (at Card.tsx:508) in RCTView (at createAnimatedComponent.js:151) in AnimatedComponent (at Card.tsx:491) in PanGestureHandler (at Card.tsx:484) in RCTView (at createAnimatedComponent.js:151) in AnimatedComponent (at Card.tsx:480) in RCTView (at Card.tsx:473) in Card (at CardContainer.tsx:154) in CardContainer (at CardStack.tsx:518) in RCTView (at CardStack.tsx:109) in MaybeScreen (at CardStack.tsx:511) in RCTView (at CardStack.tsx:92) in MaybeScreenContainer (at CardStack.tsx:410) in CardStack (at StackView.tsx:384) in KeyboardManager (at StackView.tsx:382) in RNCSafeAreaView (at src/index.tsx:28) in SafeAreaProvider (at SafeAreaProviderCompat.tsx:42) in SafeAreaProviderCompat (at StackView.tsx:379) in RCTView (at StackView.tsx:378) in StackView (at createStackNavigator.tsx:67) in StackNavigator (at Navigation.tsx:15) in EnsureSingleNavigator (at BaseNavigationContainer.tsx:267) in ForwardRef(BaseNavigationContainer) (at NavigationContainer.tsx:39) in ThemeProvider (at NavigationContainer.tsx:38) in ForwardRef(NavigationContainer) (at Navigation.tsx:14) in Navigation (at App.tsx:23) in Provider (at App.tsx:19) in Provider (at App.tsx:18) in App (at renderApplication.js:40) in RCTView (at AppContainer.js:101) in RCTView (at AppContainer.js:119) in AppContainer (at renderApplication.js:39)
| software | version |
|---|---|
| @react-navigation/native | ^5.0.7 |
| @react-navigation/drawer | ^5.0.7 |
| react-native-reanimated | ^1.7.0 |
| react-native-gesture-handler | ^1.6.0 |
| react-native-safe-area-context | ^0.7.3 |
| react-native-screens | ^2.0.0-beta.13 |
| react-native | 0.61.5 |
| npm or yarn | yarn |
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.
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 :)
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
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 You can. Just useSelector and useDispatch from react-redux will help you with this.
@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.
{...props} />
Great tip dude. Thumbs up saved my day <3
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.
i was using it like
drawerContent={DrawerContent}
changed it to
drawerContent={props => <DrawerContent {...props} />}
now working.