Reputation: 485
I have to cutomize my drawer then I create a DrawerComponent with all items thas I need, I create a function with NavigationActions to dispatch my routes but it doesn't work. when I click on any item I have this error: cannot read property 'dispatch' of undefine
import {NavigationActions} from 'react-navigation';
...
class DrawerComponent extends React.Component {
navigateToScreen = (route) => () => {
const navigateAction = NavigationActions.navigate({
routeName: route
});
this.props.navigation.dispatch(navigateAction);
}
render()...
...
...
<TouchableOpacity
style={Styles.centerContent}
onPress= {this.navigateToScreen('Messages')}
>
<IconIonic name="ios-mail" size={60} color= '#fff'/>
<View>
<Text>messages</Text>
</View>
</TouchableOpacity>
In drawer
import { createStackNavigator, createDrawerNavigator, createAppContainer } from 'react-navigation';
...
const DrawerNavigation = createDrawerNavigator({
Home: { // entree (route name) : on peut la nommer comme on veut mais on prefere lui donner le meme nom que notre screen qu'on va afficher
screen: HomeStackNavigation, // le screen qu'on va afficher IL DOIT ETRE UN STACK
},
Message: {
screen: Messages,
},
},
{
drawerWidth: width*0.83,
contentComponent: props =>
{
return(<DrawerComponent/>)
},
drawerPosition: 'left',
},
);
before that error, I had something like : undefined is not an object evaluating _this.props.navigation.dispatch()
Upvotes: 4
Views: 1386
Reputation: 2784
{
contentComponent: props => {
return <DrawerComponent {...props} />;
};
}
Don't swallow props
. Pass it to child component
Upvotes: 1