Add a view in a body of the tab - react-native

I am new to react native. I have a tab but could not figure out how to add a view inside the body of a specific tab. For instance I want a list in tab "first", how can I add this component here? Thankyou in advance.
import Tabs from 'react-native-tabs';
class Example extends Component {
constructor(props){
super(props);
this.state = {page:'second'};
}
render() {
var self = this;
return (
<View style={styles.container}>
<Tabs selected={this.state.page} style={{backgroundColor:'white'}}
selectedStyle={{color:'red'}} onSelect={el=>this.setState({page:el.props.name})}>
<Text name="first">First</Text>
<Text name="second" selectedIconStyle={{borderTopWidth:2,borderTopColor:'red'}}>Second</Text>
<Text name="third">Third</Text>
<Text name="fourth" selectedStyle={{color:'green'}}>Fourth</Text>
<Text name="fifth">Fifth</Text>
</Tabs>
<Text style={styles.welcome}>
Welcome to React Native
</Text>
<Text style={styles.instructions}>
Selected page: {this.state.page}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('Example', () => Example);

The react-native-tabs package takes each child of the <Tabs> tag and wraps it into a separate component that will only be shown when the tab is selected. So what you have to do is the following:
render() {
var self = this;
return (
<View style={styles.container}>
<Tabs selected={this.state.page} style={{backgroundColor:'white'}}
selectedStyle={{color:'red'}} onSelect={el=>this.setState({page:el.props.name})}>
<View name="view">
<Text>Text Wrapped inside a view</Text>
</View>
<View name="custom component" selectedIconStyle={{borderTopWidth:2,borderTopColor:'red'}}>
<MyCustomComponent/>
</View>
<Text name="third">Third</Text>
<Text name="fourth" selectedStyle={{color:'green'}}>Fourth</Text>
<Text name="fifth">Fifth</Text>
</Tabs>
</View>
);
Everything rendered outside the <Tabs> tag will appear no matter which tab is selected.

Related

text above row of buttons in React Native

This should be relatively easy for a React Native expert. I simply wish to display some text, and then under the text should be a row of buttons. Beginning with the row of buttons, this seems to work fine:
constructor(props) {
super(props);
this.state = {
titleText: "Bird's Nest",
bodyText: "This is not really a bird nest."
};
}
render() {
return (
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Camera"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Start"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Website"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Stop"/>
</View>
</View>
); //return
} //render
const styles = StyleSheet.create({
page:{
flex:1,
backgroundColor: "#000",
paddingTop:50,
color: 'white'
},
container: {
flex: 1,
backgroundColor: "#000",
flexDirection: 'row',
alignItems: 'center',
justifyContent: "center"
},
buttonContainer: {
flex: 1,
},
container1: {
backgroundColor: 'powderblue',
height : 100
},
container2: {
backgroundColor: 'grey',
height : 100
},
baseText: {
fontFamily: "Cochin"
},
titleText: {
fontSize: 20,
fontWeight: "bold",
color: 'white'
}
});
When I attempt to add the text...it is functional however the text is positioned before the row of buttons instead of placement over/above the row of buttons. Here is the same code inclusive of the text:
...
render() {
return (
<View style={styles.container}>
<Text style={styles.baseText}>
<Text style={styles.titleText} onPress={this.onPressTitle}>
{this.state.titleText}
{"\n"}
{"\n"}
</Text>
<Text numberOfLines={5}>{this.state.bodyText}</Text>
</Text>
<View style={styles.buttonContainer}>
<Button title="Camera"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Start"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Website"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Stop"/>
</View>
</View>
); //return
} //render
...
So basically the 'text' is rendered and then the 4 buttons are rendered with everything all 'crushed' together...all within the same row. There probably needs to be some sort of 'column' for the text however I have no idea how to combine 'column' and 'row' (necessary for the buttons) in React Native (within the same page 'container'). Or perhaps there is some other way to do it?
In addition, it would be nice if the row of buttons appeared along the bottom of the device screen instead of centered. I had changed the 'alignItems' in the 'styles.container' to 'baseline' (before I added any text) however that did not accomplish the desired result. I thank you in advance for any advice.
UPDATE:
At this point I have modified my code above and what I have now is something more along the lines of what I am seeking...although not perfect. There is probably a better way however here is what my code looks like now:
import BoxContainer from './BoxContainer';
render() {
return (
<View style={styles.page}>
<BoxContainer style={styles.container1}>
<Text>Bunch of text here.</Text>
</BoxContainer>
<BoxContainer style={styles.container2}>
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Camera"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Start"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Website"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Stop"/>
</View>
</View>
</BoxContainer>
</View>
); //return
} //render
The code for 'BoxContainer' is here, a resource I found on the Internet somewhere:
import React from 'react';
import { StyleSheet, View} from 'react-native';
const BoxContainer = props => {
return (
<View style={{...styles.boxContainer, ...props.style}}>
{props.children}
</View>
);
};
const styles = StyleSheet.create({
boxContainer:{
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.8,
shadowRadius: 2,
height:200,
margin: 20,
alignItems: 'center',
justifyContent: 'center'
}
});
export default BoxContainer;
As mentioned, this is close to what I am seeking however I wish I could find a way to do it without the structured box containers...or perhaps that is necessary in React Native...?

Nested Texts not aligning (React Native)

I have this basic setup in React Native:
class App extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.textContainer}>
<View>
<Text style={styles.textHighlighting}>
Testing
</Text>
</View>
.
</Text>
<Text style={styles.textContainer}>
<View>
<Text style={styles.textHighlighting}>
Again
</Text>
</View>
!
</Text>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
textContainer: {
fontSize: 50
},
textHighlighting: {
backgroundColor: "yellow",
borderRadius: 10,
fontSize: 50
}
})
I am wrapping the inner text in a view because I want it to be highlighted and to be able to add a borderRadius. This seems to break when I don't have the View. But the text I don't have within the View seems to align itself lower vertically than the highlighted text.
It seems to work fine on IOS. Just not on Android.
Check this expo snack https://snack.expo.dev/#gaurav1995/carefree-truffle
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
export default function App() {
return (
<View style={styles.container}>
<View style={styles.textContainer}>
<Text style={styles.textHighlighting}>
Testing
</Text>
<Text style={styles.newFont} >.</Text>
</View>
<View style={styles.textContainer}>
<Text style={styles.textHighlighting}>
Again
</Text>
<Text style={styles.newFont} >!</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
textContainer: {
flexDirection:'row',
marginVertical:5,
padding:2
},
textHighlighting: {
backgroundColor: "yellow",
borderRadius: 10,
fontSize: 50,
},
newFont:{
fontSize:50,
marginLeft:2
}
});
check this out
render() {
return (
<View style={styles.container}>
<Text style={styles.textContainer}>
<Text style={styles.textHighlighting}>Testing</Text>.
</Text>
<Text style={styles.textContainer}>
<Text style={styles.textHighlighting}>Again</Text>!
</Text>
</View>
);
}
}

How to justify side-by-side buttons to centre with spacing between them?

My page currently!
Hi guys, I made these 2 buttons and I want them to be justified to the center, have rounded edges as well as have some spacing between them. This is my code currently:
export default class SortingComponent extends Component {
render() {
return (
<ScrollView>
<View>
<Text style = {styles.heading}>
Find Clinics By:
</Text>
</View>
<View style={{ flexDirection:"row" }}>
<View style = {styles.buttonStyle}>
<Button
title="Total Time"
onPress = {() => console.log("Sort by Total Time")}
>Total Time</Button>
</View>
<View style = {styles.buttonStyle}>
<Button
title="Travel Time"
onPress = {() => console.log("Sort by Travel Time")}
>Travel Time</Button>
</View>
</View>
</ScrollView>
)
}
}
const styles = StyleSheet.create({
buttonStyle: {
fontSize: 20,
fontWeight: "bold",
alignItems: "center",
},
heading: {
fontSize: 35,
fontWeight: "bold",
}
});
Add justify-content : 'space-evenly' in view like
<View style={{ flexDirection:"row", justify-content : 'space-evenly'}}>
.....
</View>

react-native position element at the bottom

I am trying to move a react native component called "forum" to the bottom of the screen, I tried position: fixed, bottom:0 but it didn't work
I also tried 'flex-end' and still didn't work
any idea what's wrong?
here is the expo code:
import React from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { DrawerNavigator } from 'react-navigation';
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold', marginTop: 20 }}>Home</Text>
</View>
);
}
}
class Enquiry extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold', marginTop: 20 }}>Enquiry</Text>
</View>
);
}
}
class Batches extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold', marginTop: 20 }}>Batches</Text>
</View>
);
}
}
class Forum extends React.Component {
render() {
return (
<View style={{ flex:1,justifyContent:'center',alignItems: 'center',position:'absolute',bottom:0}}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold',marginBottom:36}}>Forum</Text>
</View>
);
}
}
const MyDrawerNavigator = new DrawerNavigator(
{
Home: HomeScreen,
Batches:Batches,
Enquiry:Enquiry,
Forum:Forum,
},
{
drawerBackgroundColor: 'rgba(255,255,255,.9)',
contentOptions: {
activeTintColor: '#fff',
activeBackgroundColor: '#6b52ae',
},
}
);
export default MyDrawerNavigator;
I am not allowed to share images but click the link to see
enter image description here
The valid position option in react native is relative or absolute. The default value is relative.
If you want scroll content and fix position of Forum in your screen, you have to separate your view hierarchy like the following and use absolute position.
<View>
<ScrollView>
...anything content
</ScrollView>
<Forum style={{position:'absolute', bottom: 0}}/>
</View>
<...>
<MyDrawerNavigator .../>
<Forum style={{position:'absolute', bottom: 0}} .../>
<.../>

How to vertically aligned my image to top in my React-native component

I'd like to top aligned my image in React-native. But don't know what to do. Here is my layout and its style:
class layout extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.toolbar}>
<Text style={styles.left}>
Left Button
</Text>
<Text style={styles.title}>
This is my title
</Text>
<Text style={styles.right}>
Right Button
</Text>
</View>
<View style={styles.content}>
<Image style={styles.image}
source={require('./back.jpg')}
resizeMode="contain"
></Image>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
toolbar: {
backgroundColor: '#B5AC3a',
height:64,
flexDirection:'row'
},
left: {
marginTop:30,
fontSize: 14,
textAlign: 'center'
},
right: {
marginTop:30,
fontSize: 14,
textAlign: 'center'
},
title: {
marginTop:30,
flex: 1,
fontSize: 14,
textAlign: 'center'
},
content: {
backgroundColor:'#ffecff'
},
image: {
width: Dimensions.get('window').width - 20,
margin: 10,
alignSelf:'center'
}
});
Here is the result:
Apparently there are a lot of space between my image to top of my screen. I tried: flex-start. But doesn't work. What should I do? Thanks.
P.S: It's fine if I use View instead of Image like this:
class layout extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.toolbar}>
<Text style={styles.left}>
Left Button
</Text>
<Text style={styles.title}>
This is my title
</Text>
<Text style={styles.right}>
Right Button
</Text>
</View>
<View style={styles.content}>
<View style={styles.messageBox}>
<Text>This is line one</Text>
<Text>This is line two</Text>
</View>
</View>
</View>
);
}
}
Result:
Your resizeMode='contain' on your image is conflicting with the dimensions you want to define in styles.image.
Simply remove resizeMode='contain' in your <Image> properties and it should work.
Something else: I would strongly advise you, depending on what you're trying to do, to use a ScrollView instead of the simple View when you have extendable content (namely anything that isn't completely static). This seems to be what you need.