React native Drawer Navigator in dynamic image and name - react-native

I am new in react native and I want to show data from server I used with below code and don't know how to use here fetch method for call api.
This is drawer navigator:
const AppDrawerNavigator = DrawerNavigator({
Logout: {
screen: Login,
navigationOptions: {
drawerLabel: 'Logout',
drawerIcon: () => (
<Icon name="sign-out" size={18} color="#fff" />
)
},
}
// Test: Test,
},
{
drawerBackgroundColor: "black",
contentComponent: CustomDrawerContentComponent,
contentOptions: {
activeTintColor: '#fff',
inactiveTintColor: '#fff',
activeBackgroundColor: '#f673d7',
itemStyle: {
fontSize: 18,
},
},
});
I want to use dynamic image and name in below component:
const CustomDrawerContentComponent = (props) => (
<View>
<ScrollView>
<View style={styles.profileBg}>
<ImageBackground style={{width: Platform.OS === 'ios' ? 190 : width/1.5, height: 210}} source = {require('../images/banner-2.jpg')}>
<View style={styles.profileHeader}>
<TouchableHighlight>
<View style={styles.profilepicWrap}>
<Image style={styles.profilePic}
source={require('../images/Female-Avatar.png')}
/>
</View>
</TouchableHighlight>
<Text style={styles.name}>Rahul Mishra</Text>
<Text style={styles.changePassword}><Icon name="map-marker" size={16} color="#fff" style={{paddingRight:5}} /> Miamibeach, FL</Text>
</View>
</ImageBackground>
</View>
<DrawerItems
{...props}
getLabel = {(scene) => (
<View style={{borderBottomWidth:1,borderBottomColor: "#fff",width:width/1.9}}>
<Text style={{color:'#fff',fontSize:18,paddingBottom:10,paddingTop:10}}>{props.getLabel(scene)}</Text>
</View>
)}
/>
</ScrollView>
</View>
);
In above code I used const AppDrawerNavigator for calling DrawerNavigator and for contentComponent I used CustomDrawerContentComponent so I am very confusing here how to use API method here.

You can create a component and call this in content component like below:
DrawerUserDetail is a separate component and you can code anything there as normally we are doing...That works for me.
contentComponent: (props) => (
<View>
<ScrollView>
<DrawerUserDetail navigation={props.navigation} />
<DrawerItems
{...props}
getLabel = {(scene) => (
<View style={{borderBottomWidth:0.5,borderBottomColor: "grey",width:width/1.9}}>
<Text style={{color:'#333',fontSize:18,paddingBottom:14,paddingTop:14}}>{props.getLabel(scene)}</Text>
</View>
)}
/>
<DrawerCustom navigation={props.navigation} />
</ScrollView>
</View>
)

Related

I can't go back to previous screen with navigation.goBack() in react-native

I have the Profile tab with it's children. From ChildA I navigate to ChildB, but when I'm trying to go back to Profile, it won't work. I have tried navigation.goBack(null) and useNavigation() hook but without any luck. This is the code I have so far:
ChildA
const ChildA = ({navigation}) =>{
const onClick = (item) =>{
navigation.navigate("ChildB",{
photo:item.uri
})
}
return (
<View style={styles.container}>
<FlatList
data={images}
numColumns={2}
showsVerticalScrollIndicator={false}
style={styles.list}
contentContainerStyle={{ alignItems: 'center', }}
renderItem={({ item }) => (
<TouchableOpacity onPress={()=>onClick(item)}>
<Image
source={item}
style={styles.listItem}
keyExtractor={(item) => item.key}
/>
</TouchableOpacity>
)}
/>
</View>
)
}
ChildB
const ChildB = ({ navigation, route }) => {
console.log(route.name)
const {photo} = route.params
const DEVICE = Dimensions.get("screen")
return (
<View style={styles.container}>
<BackButton onPress={()=>navigation.goBack()} />
<Image source={{ uri: photo }} style={{ width: DEVICE.width, height: DEVICE.height / 2 }} />
<Text>This is the comments section</Text>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
position: 'absolute'
}
})
App.jsx
function Navigation(): JSX.Element {
return (
<Tab.Navigator
shifting={false}
activeColor="#315399"
// inactiveColor="#3e2465"
barStyle={{ backgroundColor: "white" }}>
<Tab.Screen
name="TabA" />
),
}}
component={TabA}
/>
<Tab.Screen
name="TabB" />;
},
}}
component={TabB}
/>
<Tab.Screen
name="Profile" />
),
}}
component={Profile}
/>
</Tab.Navigator>
);
}
const Tab = createMaterialBottomTabNavigator();
const Stack = createStackNavigator();
function App(): JSX.Element {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Login">
{/* Auth Navigator: Include Login and Signup */}
<Stack.Screen
name="Login"
component={Login}
options={{ headerShown: false }}
/>
<Stack.Screen
name="Register"
component={Register}
options={{headerShown:false}}
/>
<Stack.Screen name="ChildB"
component={ChildB}
options={{headerShown:false}}/>
<Stack.Screen
name="ForgotPassword"
component={ForgotPassword}
options={{headerShown:false}}
/>
<Stack.Screen
name="Navigation"
component={Navigation}
// Hiding header for Navigation Drawer
options={{headerShown:false}}
// options={({ route }) => ({
// headerTitle: getHeaderTitle(route),
// })}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
How can I go back from ChildB to Profile? I'm new to react-native so any advice would be appreciated!
I have managed to make it work by passing the navigation as prop instead of the simple function prop I was using.Since I was using a customised back button, the prop function wasn't triggered from screen to stack, only between screens(e.g from Register back to Login).
BackButton before:
export default function BackButton({goBack}) {
return (
<TouchableOpacity onPress={goBack} style={styles.container}>
<FontAwesomeIcon icon={faArrowLeft}/>
</TouchableOpacity>
)
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
top: 10 + getStatusBarHeight(),
left: 4,
zIndex:1
},
image: {
width: 24,
height: 24,
},
})
and used it in another component by passing the prop navigation to that component and then passing the navigation.goBack to the goBack from BackButton:
const componentA = ({navigation})=>{
return(
<View>
<BackButton goBack={navigation.goBack}
</View>
)}
BackButton working:
export default function BackButton({navigation}) {
return (
<TouchableOpacity onPress={navigation.goBack} style={styles.container}>
<FontAwesomeIcon icon={faArrowLeft}/>
</TouchableOpacity>
)
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
top: 10 + getStatusBarHeight(),
left: 4,
zIndex:1
},
image: {
width: 24,
height: 24,
},
})
and Component:
const componentA = ({navigation})=>{
return(
<View>
<BackButton navigation={navigation}
</View>
)}

How to put an image in a react-native drawer?

I am using the #react-navigation/native library. I have a drawer with some screens in them. I can click on the links and navigate to the appropriate screen. I would like to place an image above the links in the drawer. I am using the property drawerContent to do this. However, when I do this it removes the links. I cant figure out how to have the links and the image above the links in the drawer. What is the correct way to do this? Here is some code for my drawer with the drawerContent.
<NavigationContainer>
<Drawer.Navigator
initialRouteName="Screen1"
drawerContent={() => (
<View style={{height: "10%"}}>
<Image
style={{width: "90%"}}
resizeMode="contain"
source={require('../../../assets/new-logo/textSide.png')}/>
</View>
)}
screenOptions={ ({navigation}) => ({
drawerContainerStyle: {paddingTop: 100},
drawerLabelStyle: {
fontSize: KbStyles.properties.drawerLabelSize,
color: "white"
},
headerTintColor: {color: KbStyles.properties.white},
headerStyle: {
backgroundColor: KbStyles.properties.black
},
headerTitleStyle: {
fontSize: KbStyles.properties.headerSize,
color: "white"
},
headerLeft: () => (
<TouchableWithoutFeedback
onPress={() => {navigation.toggleDrawer()}}>
<Image
resizeMode="contain"
style={{height: "50%"}}
source={require("../../../assets/menu.png")}
/>
</TouchableWithoutFeedback>
),
drawerActiveBackgroundColor : KbStyles.properties.red,
drawerActiveTintColor: KbStyles.properties.white,
drawerStyle: {
backgroundColor: "black",
width: 300
}
})}
>
<Drawer.Screen name="Screen1" component={Screen1} />
<Drawer.Screen name="Screen2 component={Screen2} />
<Drawer.Screen name="Screen3" component={Screen3} />
</Drawer.Navigator>
</NavigationContainer>
Here is also an image showing what I would like to achieve.
What is the correct way to do this?
You can try this
Under the Drawer.Navigator you can use drawerContent as,
drawerContent={(props)=> {
return(
<View style={{flex:1}}>
<DrawerContentScrollView {...props}>
<ImageBackground source={require("../Assets/Vrda1img2.jpg")} style={{justifyContent:"space-between",alignItems:"center",padding:20,marginBottom:20,backgroundColor:"rgb(0,0,0)",borderBottomWidth:2,borderColor:Colors.secondary}} imageStyle=
{{opacity:0.4}}>
<Image source={require("../Assets/vector.png")} style={{width:70,height:70,borderRadius:35,borderWidth:2,borderColor:Colors.white}}/>
<Text style={{fontSize:20,fontWeight:"bold",color:Colors.white}}>{userDetail?userDetail.name:"Not Available"}</Text>
<Text style={{color:Colors.light}}>{userDetail?userDetail.email:"Not Available"}</Text>
</ImageBackground>
<DrawerItemList {...props}/>
</DrawerContentScrollView>
<TouchableOpacity onPress={()=>{logout()}} style={{position:"relative",right:0,left:0,bottom:5,backgroundColor:"rgb(231,230,230)"}}>
<Text style={{backgroundColor:"rgba(162,160,160,0.29)",width:"100%",height:40,textAlign:"center",paddingTop:8}}><SimpleLineIcons name={"logout"} size={15} color={Colors.primary}/> LogOut</Text>
</TouchableOpacity>
</View>
)
}
}
I give you the reference of my code where you can add Image under the Image background you can also use Uri to add an image as a link.

How to navigate from a component class in React Native

I want to simply navigate from Home Screen to Creator Screen. I can easily navigate between them if i place the link directly in my Home Class, but I have created a Cards component class for all my creators, now I can't navigate from my Cards Class. Any idea how? The code in below is not working. I got error said that Can't find variable: navigation.
my Home scree screenshot
Any one would help? Big thanks.
I tired to make my Cards Class like this, but then my imageUri became a new problem. So i removed it, the current Cards Class is in below.
const Cards = ({navigation}) => (
<View style={{padding: 30, flexDirection: 'row', borderBottomColor: '#6a6a6a', borderBottomWidth: 0.4}}>
<View>
<Image source={this.props.imageUri} style={styles.profileIcon} />
<Image source={images.broShakeLogo} style={styles.broShakeLogo} />
</View>
<View style={{paddingLeft: 40, flexDirection: 'column'}}>
.....
<TouchableOpacity
onPress={() => navigation.navigate('creator')} >
.....
);
Here's my stack navigator
const HomeStack = createStackNavigator ({
home: {
screen: HomeScreen,
},
creator: {
screen: CreatorScreen
},
},
{
headerMode: 'none'
});
And here's my Home Class
class Home extends Component {
return (
<View style={styles.container}>
<ScrollView>
<View style={styles.welcomContainer}>
{/* so this one in here actually works fine but i want them wrap inside my Cards Class */}
<TouchableOpacity onPress={() => this.props.navigation.navigate('creator')}>
<CT.ReadmoreText/>
</TouchableOpacity>
</View>
<Cards imageUri={require('../assets/images/cprofile_2.jpg')} distance="30" navigation={this.props.navigation}/>
<Cards imageUri={require('../assets/images/cprofile_1.jpg')} distance="34" />
<Cards imageUri={require('../assets/images/cprofile_1.jpg')} distance="20" />
<Cards imageUri={require('../assets/images/cprofile_3.jpg')} distance="10" />
</ScrollView>
</View>
);
}
}
export default Home;
Here's my Cards class
class Cards extends Component {
render() {
<View style={styles.container}>
<View style={{padding: 30, flexDirection: 'row', borderBottomColor: '#6a6a6a', borderBottomWidth: 0.4}}>
<View>
<Image source={this.props.imageUri} style={styles.profileIcon} />
<Image source={images.broShakeLogo} style={styles.broShakeLogo} />
</View>
<View style={{paddingLeft: 40, flexDirection: 'column'}}>
<Icon name="location-arrow" type="FontAwesome"
style={{ color: "#00d278" ,fontSize: 18 }} >
<Text style={styles.distanceText}> {this.props.distance} m</Text>
</Icon>
<Text style={styles.locationText}>Atomica</Text>
<Text style={styles.cityText}>MELBOURNE</Text>
<View style={{paddingTop: 18}}>
<TouchableOpacity
onPress={() => navigation.navigate('creator')}
>
<CT.ReadmoreText/>
</TouchableOpacity>
</View>
</View>
</View>
</View>
);
}
}
Your can try this
import {withNavigation} from 'react-navigation'
class Home extends React.Component{
return (
<View style={styles.container}>
<ScrollView>
<View style={styles.welcomContainer}>
{/* so this one in here actually works fine but i want them wrap inside my
Cards Class */}
<TouchableOpacity onPress={() =>
this.props.navigation.navigate('creator')}>
<CT.ReadmoreText/>
</TouchableOpacity>
</View>
<Cards imageUri={require('../assets/images/cprofile_2.jpg')} distance="30"
/>
<Cards imageUri={require('../assets/images/cprofile_1.jpg')} distance="34"
/>
<Cards imageUri={require('../assets/images/cprofile_1.jpg')} distance="20"
/>
<Cards imageUri={require('../assets/images/cprofile_3.jpg')} distance="10"
/>
</ScrollView>
</View>
);
}
}
export default withNavigation(Home);
const Cards = () => (
<View style={{padding: 30, flexDirection: 'row', borderBottomColor: '#6a6a6a',
borderBottomWidth: 0.4}}>
<View>
<Image source={this.props.imageUri} style={styles.profileIcon} />
<Image source={images.broShakeLogo} style={styles.broShakeLogo} />
</View>
<View style={{paddingLeft: 40, flexDirection: 'column'}}>
.....
<TouchableOpacity
onPress={() => this.props.navigation.navigate('creator')} >
.....
);
You have passed navigation as props,
<Cards imageUri={require('../assets/images/cprofile_2.jpg')} distance="30" navigation={this.props.navigation}/>
So, you need to use it as this.props.navigation.navigate('....');
but you have used it as navigation.navigate('....');
change it and it will work.

Drawer Navigator Children Props

Is there a way for me to get the props from a child of my DrawerNavigator inside my CustomDrawerComponent ?
When I open my Drawer, I wanted to get my StackNavigator screens inside it, not simply "AppStackNavigator". Is there a easy way do do that ?
My Navigators:
const AppStackNavigator = createStackNavigator(
{
Início: {
screen: HomeScreen
},
Perfil: {
screen: ProfileScreen
},
Notificações: {
screen: NotificationScreen
},
'Criar Evento': {
screen: CreateEventScreen
},
EventScreen
},
StackNavigatorConfig()
)
const AppNavigator = createDrawerNavigator(
{
AppStackNavigator
},
{
contentComponent: Drawer,
drawerBackgroundColor: color.primary.main
}
)
const AppContainer = createAppContainer(AppNavigator)
My CustomDrawerContentComponent:
export default (CustomDrawerContentComponent = props => {
return (
<ScrollView>
<TouchableOpacity onPress={() => props.navigation.closeDrawer()} style={styles.close}>
<EvilIcons style={{ color: color.primary.contrastLightText }} size={40} name="close" />
</TouchableOpacity>
<View style={styles.thumbImageContainer}>
<ThumbImage image={require('../assets/images/user.jpg')} />
<View style={styles.statusContainer}>
<TextApp>Luis Coimbra</TextApp>
<TextApp secondary>Apaixonado por Jesus</TextApp>
</View>
</View>
<SafeAreaView style={{ flex: 1 }} forceInset={{ top: 'always', horizontal: 'never' }}>
<DrawerItems {...props} {...itemsStyle} />
</SafeAreaView>
</ScrollView>
)
})
With the props.navigation.state.routes[0].routes.slice(-1)[0].routeName, I can manage to get the active router so I am able to style. If you have a better way, I'd be glad to read.
Not exactly what I was expecting, but it works well for now:
export default (CustomDrawerContentComponent = props => {
const activeRouterName = props.navigation.state.routes[0].routes.slice(-1)[0].routeName
return (
<ScrollView>
<TouchableOpacity onPress={() => props.navigation.closeDrawer()} style={styles.close}>
<EvilIcons style={{ color: color.dark.contrast }} size={40} name="close" />
</TouchableOpacity>
<View style={styles.thumbImageContainer}>
<ThumbImage source={require('../assets/images/user.jpg')} />
<View style={styles.statusContainer}>
<TextApp dark>Luis Coimbra</TextApp>
<TextApp secondary>Apaixonado por Jesus</TextApp>
</View>
</View>
<SafeAreaView
style={{ flex: 1, borderTopWidth: 2, borderTopColor: color.dark.contrast }}
forceInset={{ top: 'always', horizontal: 'never' }}
>
{['Início', 'Perfil', 'Notificações', 'Criar Evento'].map(routerName => (
<View key={routerName} style={routerName == activeRouterName && styles.activeView}>
<TextApp
onPress={() => props.navigation.navigate(routerName)}
style={{
color:
routerName == activeRouterName
? color.secondary()
: color.dark.contrast,
margin: 16,
fontWeight: 'bold'
}}
>
{routerName}
</TextApp>
</View>
))}
</SafeAreaView>
</ScrollView>
)
})
Result:

In React Native, I want to Add Dynamic Drawer Navigation Item which data come from api(fetch)

Here is my Static code:
class App extends Component {
render() {
return (
<MyApp />
)
}
}
const CustomDrawerContentComponent = (props) => (
<View style={{position:'relative', height:'100%'}}>
<View style={styles.drawerHeader}>
<ImageBackground style={styles.back} source={require('./src/Header/banner.jpg')}>
<View style={{flex:1, justifyContent:'center', alignItems:'center'}}>
<Image
style={styles.drawerImage}
source={require('./src/shirsho.png')} />
</View>
</ImageBackground>
</View>
<View>
{/* <DrawerItems {...props} /> */}
<DrawerItems {...props}
activeTintColor='#000'
activeBackgroundColor='#e3e3e3'
inactiveTintColor='rgba(0, 0, 0, .87)'
inactiveBackgroundColor='transparent'
itemsContainerStyle= {{ marginTop: 0,}}
itemStyle={{ borderBottomWidth: 2, borderBottomColor: '#eee',}}
labelStyle={{fontSize: 16,color: '#000'}}/>
</View>
<View style={{width:100,height:50, position:'absolute', bottom:0,right:30}}>
<Image
style={{width:100, marginLeft: 10,}}
source={require('./src/bdtask.png')} />
</View>
</View>
);
const MyApp = DrawerNavigator({
'হোম' : {
screen : Homepage,
},
'জাতীয়' :{
screen : NationalTab,
},
'খেলা' :{
screen : PlayTab,
},
'প্রশাসন' :{
screen : AdminTab,
},
'অর্থনীতি' :{
screen : EconomicsTab,
},
'বিনোদন' :{
screen : EntertainmentTab,
},
'শিক্ষা' :{
screen : EducationTab,
}
},{
initialRouteName: 'হোম',
drawerPosition: 'left',
contentComponent: CustomDrawerContentComponent,
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle'
})
export default App
Also, I want to pass value as props with this drawer item.