I'm new to react native development and I have a problem hope someone can help me!
I'm trying to show some components in a screen, I'm using native base and when I try to put the HomeHeader component and the MainSection component together in the Home Screen, They show with a huge space between them and if I try to duplicate the components, white space is shown between them also!
I don't know why this happens and I hope if someone can help me
Attached code and screenshots for the result on the genymotion emulator
Thanks in advance!
Home.js
import React from "react";
import { Container, Header, Content } from "native-base";
import { ImageBackground, Image, ScrollView } from "react-native";
import MainSection from "../Components/MainSection";
import HomeHeader from "../Components/HomeHeader";
export default class Home extends React.Component {
static navigationOptions = {
header: null
};
render() {
return (
<Container>
<Content>
<HomeHeader />
<MainSection />
<MainSection />
<MainSection />
</Content>
</Container>
);
}
}
HomeHeader.js
import React from "react";
import { Container, Text } from "native-base";
import { Image, ImageBackground } from "react-native";
export default class HomeHeader extends React.Component {
render() {
return (
<Container>
<ImageBackground
source={require("../Assets/Backgrounds/home-header.png")}
style={{
width: null,
flex: 1,
height: 130,
flexDirection: "row",
justifyContent: "center"
}}
>
</ImageBackground>
</Container>
);
}
}
MainSection.js
import React from "react";
import { Container, Content, Text, Button, View } from "native-base";
import { ImageBackground, Image, ScrollView } from "react-native";
export default class MainSection extends React.Component {
render() {
return (
<Container>
<Content>
<View
style={{
flex: 1,
flexDirection: "row",
marginTop: 13
}}
>
<Image
source={require("../Assets/Vector_Icons/home-first-icon.png")}
style={{ height: 50, width: 50, marginLeft: 25 }}
/>
<Text style={{ marginTop: 15, marginLeft: 10, marginRight: 50 }}>
Places For Going Out
</Text>
<Button transparent success style={{ marginTop: 3 }}>
<Text>View More</Text>
</Button>
</View>
<View
style={{
flex: 1,
flexDirection: "row",
marginTop: 13,
marginLeft: 25
}}
>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
>
<Image
source={require("../Assets/Backgrounds/splash-bg.png")}
style={{
width: 150,
height: 150,
borderRadius: 5,
marginRight: 10
}}
/>
<Image
source={require("../Assets/Backgrounds/splash-bg.png")}
style={{
width: 150,
height: 150,
borderRadius: 5,
marginRight: 10
}}
/>
<Image
source={require("../Assets/Backgrounds/splash-bg.png")}
style={{ width: 150, height: 150, borderRadius: 5 }}
/>
</ScrollView>
</View>
</Content>
</Container>
);
}
}
Result
Related
I have been simply trying to render a card from react-native-elements UI library. Here is the documentation that I've been looking through and literally copied and pasted from: https://reactnativeelements.com/docs/1.2.0/card
Here is my Deal component:
import { View } from 'react-native'
import { Card, ListItem } from 'react-native-elements'
const Deal = () => {
const users = [
{
name: 'brynn',
avatar: 'https://www.w3schools.com/howto/img_avatar2.png',
},
]
return (
<View>
<Card containerStyle={{ padding: 0 }}>
{users.map((u, i) => {
return (
<ListItem
key={i}
roundAvatar
title={u.name}
leftAvatar={{ source: { uri: u.avatar } }}
/>
)
})}
</Card>
</View>
)
}
export default Deal
Here is my SecondScreen component in which I am trying to render Deal:
import { StyleSheet, Text, View } from 'react-native'
import Step from '../components/Step'
import MyCarousel from '../components/MyCarousel'
import Ratings from '../components/Ratings'
import Deal from '../components/Deal'
export default function SecondScreen({ route, navigation }) {
const { image } = route.params
return (
<>
<View>
<Text
style={{ fontSize: '16px', marginLeft: 10, marginTop: 15 }}
>
Daily Deals
</Text>
<View
style={{
borderBottomColor: 'black',
borderBottomWidth: StyleSheet.hairlineWidth,
}}
/>
<View style={{ marginTop: 10 }}>
<Deal />
</View>
</View>
</>
)
}
Here is what it renders at the end:
Any help would be appreciated!
Edit: I have decided to use another component in the meantime. This seems like possibly deprecated component or something.
Try adding flex or height for <View />
<View style={{ flex: 1, marginTop: 10 }}>
<Deal />
</View>
Inside your Deal component, you have a <View /> wrapper around the <Card /> component. The one inside Deal file, NOT the one wrapping it in SecondScreen file.
Have you tried removing that excess <View />?
If you want to keep that <View />, have you tried giving that specific <View /> a style={{ height: 100px }} or style={{ flex: 1 }} property?
I am trying to create 6 customized button (3 buttons in each row) using TouchableOpacity. I was able to add the 3 buttons in the first row but when try to add the other three buttons in the next row, for some reason it pushes the the first row up. I want the gap/height between each rows to be at most 10px.
and my other question is, when I try to wrap the customized card with TouchableOpacity, it ignores the width I specify on the StyleSheet.
I am new to react native. So, if this is dumb question to ask, I apologize.
Here is the code I have,
//Card component
import React from "react";
import { StyleSheet, View, TouchableOpacity } from "react-native";
function Card(props) {
return (
<View style={{ ...styles.CardSytle, ...props.style }}>
{props.children}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
CardSytle: {
shadowColor: "black",
shadowOffset: { width: 0, height: 2 },
shadowRadius: 6,
shadowOpacity: 0.26,
elevation: 10,
backgroundColor: "white",
padding: 20,
borderRadius: 10,
marginHorizontal: 10,
marginVertical: 10,
},
});
export default Card;
//MainPage
import React from "react";
import { View, StyleSheet, Dimensions } from "react-native";
import Card from "../components/Card";
const MainPageScreen = (props) => {
return (
<View style={styles.body}>
<View style={styles.cardContainer}>
<Card style={styles.card} />
<Card style={styles.card} />
<Card style={styles.card} />
</View>
<View style={styles.cardContainer}>
<Card style={styles.card} />
<Card style={styles.card} />
<Card style={styles.card} />
</View>
</View>
);
};
const styles = StyleSheet.create({
body: {
flex: 1,
backgroundColor: "white",
},
cardContainer: {
flex: 1,
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
},
card: {
width: Dimensions.get("window").width / 3,
maxWidth: "28%",
height: 150,
maxHeight: "28%",
},
});
export default MainPageScreen;
//Here is screenshot of the buttons
[1]: https://i.stack.imgur.com/9z84U.png
You are setting a maxHeight of 28% which is setting the button height to 28% of parent and creates a blank space under the row. So you should remove it.
card: {
width: Dimensions.get('window').width / 3,
maxWidth: '28%',
height: 150,
},
As for the touchable opacity, Rather than wrapping the view you can use it as the main wrapper and style it Like below.
function Card(props) {
return (
<TouchableOpacity style={[styles.CardSytle, props.style]}>
{props.children}
</TouchableOpacity>
);
}
I am trying to create a scroll view in react native.
I gave full-device-width and height for the scroll-view.
width is working but height is not working so the app is showing only on down part of the emulator,
I would like to know how can I make this showing up in fullscreen and also why this error occurring.
this is how it's loading on emulator.
you can find my react-native code below
import React, { Component } from 'react';
// import Counterpart from './Counterpart'
import contacts from './contacts'
import {
View,
Button,
ScrollView,
Switch,
Text,
Input,
StyleSheet,
Dimensions,
} from 'react-native';
const widthfull = Dimensions.get('window').width; //full width
const heightfull = Dimensions.get('window').height; //full height
const styles = StyleSheet.create({
mainwrap: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
zIndex:1,
},
countfont: {
fontSize: 120,
},
marginfromtop: {
display: 'flex',
flex: 1,
paddingTop: 50,
},
ScrollViewstles: {
display: 'flex',
flex: 1,
margin:0,
padding:0,
zIndex:2,
width:widthfull,
height:heightfull,
paddingLeft:30
}
});
export default class App extends Component {
state = {
showCounter: true
}
toggglecounter = () => {
this.setState(() => ({showCounter: !this.state.showCounter}))
}
render() {
if (this.state.showCounter) {
return (
<View style={styles.mainwrap}>
<View style={[styles.marginfromtop, styles.countfont]}>
<Button
style={{ marginTop: 50 }}
onPress={this.toggglecounter}
title="Toggle Contacts"
/>
</View>
<View style={styles.mainwrap}>
<ScrollView style={styles.ScrollViewstles}>
{contacts.map(c => (
<Text key={c.key}>{c.name}</Text>
))}
</ScrollView>
</View>
</View>
);
} else {
return (
<View style={[styles.marginfromtop, styles.countfont]}>
<Button
style={{ marginTop: 50 }}
onPress={this.toggglecounter}
title="Toggle Contacts"
/>
</View>
);
}
}
}
remove flex: 1 from marginfromtop
marginfromtop: {
paddingTop: 50,
},
I have created a component which itself is composed of several components.Now all these components will get data dynamically and i need to repeat this main component N no of times.Here is the screenshot of the component to be repeated.
The colored panel will have some data and the heading and all text will be updated dynamically.
Here is the code which i have written so far
//FirstScreen.js
import React from 'react';
import { Text, StyleSheet, View, TouchableHighlight } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons'
import SquareCircle from './Square_circle_Button'
import CirclePlus from './circle_plus'
import DateComponent from './DateComponent'
import LibraryComponent from './LibraryComponent'
class FirstScreen extends React.Component
{
constructor(props)
{
super(props);
}
render() {
return <View style={{ flex: 1, flexDirection: "column" }}>
<DateComponent DateDisplay={this.props.DDate}/>
<View style={{ flex: 4, flexDirection: "column", justifyContent: "space-evenly" }}>
<LibraryComponent Heading={this.props.send_heading} DisplayData={this.props.DData}/>
</View>
</View>;
}
}
//LibraryComponent.js
import React from "react";
import { Text, StyleSheet, View, TouchableHighlight } from "react-native";
export default class LibraryComponent extends React.Component
{
render(){
return <View style={{ flex: 4, flexDirection: "column", justifyContent: "space-evenly" }}>
<View style={{ flex: 1, flexDirection: "column", alignItems: "center", justifyContent: "center" }}>
<Text>{this.props.Heading}</Text>
</View>
<View style={{ flex: 1, flexDirection: "column", alignItems: "center", justifyContent: "center" }}>
<Text>{this.props.DisplayData}</Text>
</View>
<View style={{ flex: 4, flexDirection: "row", alignContent: "center", justifyContent: "center" }}>
<View style={{ width: 50, height: 50, backgroundColor: "powderblue" }} />
<View style={{ width: 50, height: 50, backgroundColor: "skyblue" }} />
<View style={{ width: 50, height: 50, backgroundColor: "steelblue" }} />
<View style={{ width: 50, height: 50, backgroundColor: "gray" }} />
</View>
</View>;
}
}
//DateComponent.js
import React from "react";
import { Text, StyleSheet, View, TouchableHighlight } from "react-native";
export default class DateComponent extends React.Component
{
render()
{
return(
<View style={{ flex: 1, flexDirection: "row", alignItems: "center" }}>
<Text>{this.props.DateDisplay}</Text>
</View>);
}
}
//TimeScroll.js
import React from 'react'
import {FlatList,View} from 'react-native'
import FirstScreen from './FirstScreen'
import CirclePlus from './circle_plus'
import AddCircle from './Square_circle_Button'
export default class Timeline extends React.Component
{
render()
{
return(
<FlatList style={{flex:1}}>
data={[
{key:"20 Mar 2018"},
]}
renderItem={({item}) =><View style={{flex:1}}>
<FirstScreen send_heading={item.key} DData={item.key} DDate={item.key}/>
</View>}
<CirclePlus/>
<AddCircle/>
</FlatList>
);
}
}
If requested,Let me know how should i modify the structure to accomplish this task properly.
You just need to structure your prop data to be an array of objects where each object has the data for each color palette:
//FirstScreen.js
import React from 'react';
import { Text, StyleSheet, View, TouchableHighlight } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons'
import SquareCircle from './Square_circle_Button'
import CirclePlus from './circle_plus'
import DateComponent from './DateComponent'
import LibraryComponent from './LibraryComponent'
class FirstScreen extends React.Component
{
constructor(props)
{
super(props);
}
renderColorPalettes = () => {
const { colorData } = this.props;
return colorData.map( palette => {
return <LibraryComponent Heading={palette.heading} DisplayData={palette.data}/>
});
};
render() {
const colors = renderColorPalettes();
return <View style={{ flex: 1, flexDirection: "column" }}>
<DateComponent DateDisplay={this.props.DDate}/>
<View style={{ flex: 4, flexDirection: "column", justifyContent: "space-evenly" }}>
{colors}
</View>
</View>;
}
}
As stated if I use a custom navBar in react-native-router-flux, and style it with NO position stlyle tag, then the navBar renders at the bottom of the screen...
So, I tried setting style with position: 'absolute', top: 0, right:0, left: 0 and my navBar disappears :( Any suggestions?
Router.js
<Scene
key="api"
hideNavBar={false}
navBar={HeaderWithIcon}
component={APITest}
/>
HeaderWithIcon.js
import React, {Component} from 'react';
import { View, Image, Text } from 'react-native';
import { menuStyles } from './styles';
class HeaderWithIcon extends Component {
constructor(props) {
super(props);
}
render() {
const icon =
require('../../assets/images/myImg.png');
return (
<View>
<View style={[menuStyles.view, this.props.style]}>
<Image source={icon} style={[menuStyles.icon, this.props.iconStyle]} />
</View>
</View>
);
}
};
export default HeaderWithIcon;
styles.js
import { HEADER } from '../../global/margins';
import { PRIMARY_COLOUR, SHADOW_COLOUR } from '../../global/colours';
export const menuStyles = {
view: {
flexDirection: 'row',
//height: HEADER.height,
width: null,
backgroundColor: PRIMARY_COLOUR,
alignItems: 'center',
justifyContent: 'center',
padding: 10,
// paddingTop: HEADER.padding,
shadowColor: SHADOW_COLOUR,
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.2,
position: 'absolute',
top: 0,
left: 0,
right: 0
},
icon: {
width: HEADER.icon,
height: HEADER.icon,
alignSelf: 'center'
}
};
<View style={styles.navbarStyle}>
<View style={styles.navContainer}>
<TouchableOpacity onPress={()=>Actions.get('drawer').ref.toggle()}>
<Image source={require('../../img/menu.png')} resizeMode="contain" color="white" style={styles.menu} />
</TouchableOpacity>
<View style={styles.navbarTitle}>
<Text style={styles.title}>NavBar</Text>
</View>
<TouchableWithoutFeedback onPress={this.loginUser}>
<View style={styles.signIn}>
<Text style={styles.title}>Login</Text>
</View>
</TouchableWithoutFeedback>
</View>
</View>
And in order to style it and put it at the top ,remember to give it a position of 'absolute ' and 'top' of 0 :
navbarStyle:{
flex:1,
flexDirection:'row',
alignItems: 'center',
height ,
position: 'absolute', //necessary because if no react-native-router-flux will put navbar at bottom ,bug maybe!
top:0,
width,
backgroundColor: '#009688',
elevation:10
}
PS: for width and height ,better solution is to use Navigator and dimensions :
const {width} = Dimensions.get('window');
const height = Navigator.NavigationBar.Styles.General.NavBarHeight;
Nevermind... I realise that my HeaderView is wrapped in 2 views and I was not styling the outer one but the inner one. Leaving this here as example code for others if allowed.....
Fix:
In HeaderWithIcon.js
return (
<View style={[menuStyles.view, this.props.style]}>
<Image source={icon} style={[menuStyles.icon, this.props.iconStyle]} />
</View>
);