I want to create a custom card in react native and it gets me confused as to use the views.
I have tried to make a card following the tutorial but I am not getting anywhere since its very confusing.The image below is what I'm trying to achieve.
This is what I have
trying to make the card
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Platform } from "react-native";
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.squareShapeView}/>
<View style={{flex:0.7}}>
<Text>Test1</Text>
<Text>Test1</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
marginTop:50,
justifyContent: 'center',
alignItems:'center',
flexDirection:'row',
borderWidth:0.3,
marginLeft:30,
marginRight:30
},
squareShapeView: {
//To make Square Shape
width:20,
height:70,
backgroundColor: '#14ff5f',
alignSelf:'flex-start'
},
});
This is what I expect to get
this is what I expect
I hope this might help you
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { MaterialIcons } from '#expo/vector-icons';
import { Constants } from 'expo';
// You can import from local files
import AssetExample from './components/AssetExample';
const Card = ({ title, desc }) => (
<View style={styles.cardContainer}>
<View style={styles.cardContent}>
<View style={{ flexDirection: 'column' }}>
<Text>{title}</Text>
<Text>{desc}</Text>
</View>
<MaterialIcons name="navigate-next" size={40} color="red" />
</View>
</View>
)
export default class App extends React.Component {
constructor(props) {
super(props);
this.cards = [
{
title: 'Top up',
desc: 'Top up any number'
},
{
title: 'Top up history',
desc: 'View all of the top up you have made'
}
]
}
renderCards = () => {
return this.cards.map(card => (
<Card
title={card.title}
desc={card.desc}
/>
))
}
render() {
return (
<View style={styles.container}>
{this.renderCards()}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 25,
},
cardContainer: {
paddingTop: 30,
paddingBottom: 30,
shadowColor: 'rgba(0, 0, 0, 0.5)',
shadowOffset: { x: 0, y: 10 },
shadowOpacity: 1,
borderLeftColor: 'blue',
borderLeftWidth: 10,
alignSelf: 'stretch',
backgroundColor: 'white',
marginTop: 20,
},
cardContent: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginLeft: 20,
}
});
https://snack.expo.io/#xavier96/aGVscC
Related
I'm new to react native and trying to create a button, here is my StartScreen:
import React from 'react' import Background from '../components/Background' import AppButton from '../components/AppButton'
export default function StartScreen({ navigation }) {
return(
<Background>
<AppButton title="HEEEY!" size="sm" backgroundColor="#007bff" />;
</Background>
) }
Here is my AppButton:
import React from 'react'
import { StyleSheet, TouchableOpacity, Text } from "react-native";
export default function AppButton ({ onPress, title, size, backgroundColor }) {
return (
<TouchableOpacity
onPress={onPress}
style={[
styles.appButtonContainer,
size === "sm" && {
paddingHorizontal: 8,
paddingVertical: 6,
elevation: 6
},
backgroundColor && { backgroundColor }
]}
>
<Text style={[styles.appButtonText, size === "sm" && { fontSize: 14 }]}>
{title}
</Text>
</TouchableOpacity>
)
};
const styles = StyleSheet.create({
screenContainer: {
flex: 1,
justifyContent: "center",
padding: 16
},
appButtonContainer: {
elevation: 8,
backgroundColor: "#009688",
borderRadius: 10,
paddingVertical: 10,
paddingHorizontal: 12
},
appButtonText: {
fontSize: 18,
color: "#fff",
fontWeight: "bold",
alignSelf: "center",
textTransform: "uppercase"
}
});
Here is my Background:
import React from 'react'
import { ImageBackground, StyleSheet, KeyboardAvoidingView } from 'react-native'
import { theme } from '../core/theme'
export default function Background({ children }) {
return (
<ImageBackground style={styles.background}>
<KeyboardAvoidingView style={styles.container} behavior="padding">
{children}
</KeyboardAvoidingView>
</ImageBackground>
)
}
const styles = StyleSheet.create({
background: {
flex: 1,
width: '100%',
backgroundColor: theme.colors.surface,
},
container: {
flex: 1,
padding: 20,
width: '100%',
maxWidth: 340,
alignSelf: 'center',
alignItems: 'center',
justifyContent: 'center',
},
})
When I try to run the application in my Android phone I get the following error:
Error: Text strings must be rendered within a component.
Blockquote
But I cant figure out where this error happens. Can someone spot the mistake?
You have an unnecessary semicolon (;) in your StartScreen JSX, remove it.
export default function StartScreen({ navigation }) {
return(
<Background>
<AppButton title="HEEEY!" size="sm" backgroundColor="#007bff" />
</Background>
); }
I'm using deeplinking in react-native in order to redirect the user to a youtube channel. But when I'm going back to my app, I have a blank screen, how can I make the user going back to the homeScreen ?
Here is my youtube code :
import React from 'react';
import { View, Text, Linking } from 'react-native';
import Acceuil from './Accueil';
class ChaineYT extends React.Component {
state = {
isLoading:false,
isLinked: false
}
componentDidMount = () => {
Linking.openURL('vnd.youtube://' + 'www.youtube.com/channel/UC1UpcbyFVTZTDrvdjNmSzSg')
this.setState(isLoading=true, isLinked=true);
if(this.state.isLoading && this.state.isLinking){
return this.props.navigation.navigate("Acceuil")
}
}
render() {
return (
<View>
</View>
)
}
}
export default ChaineYT
I'm not sure about the states that I'm using.
Edit : I've updated with the Accueil.js screen.
import React from 'react';
import { StyleSheet, View, TextInput, Button, Text, FlatList, ListView, Linking, StatusBar } from 'react-native';
import voyantes from '../Helpers/voyantesData';
import VoyanteItem from './VoyanteItem';
import MyButton2 from './MyButton2';
const numColumns = 2;
class Accueil extends React.Component {
_displayDetailForVoyante = (idVoyante,photo, specialite, description, name) => {
console.log("Display film with id " + idVoyante);
this.props.navigation.navigate("VoyanteProfil", { idVoyante: idVoyante,photo: photo,specialite:specialite,description:description, name:name });
}
render() {
return (
<View style={styles.main_container}>
<View style={{ flexDirection:'row', justifyContent: 'space-around', marginVertical: 8 }}>
<MyButton2 style={{}} text={"Voyance privée\nouvert 24h/24\n01 44 01 77 01"} icone='icone-transfert' onPress={() => { Linking.openURL('tel:0144017701'); }}/>
<MyButton2 text={"Voyance sans CB\nouvert 24h/24\n32 32"} icone='icone-sonnerie' onPress={() => { Linking.openURL('tel:3232'); }}/>
</View>
<FlatList style={styles.flatList}
data={voyantes}
keyExtractor={(item) => item.id.toString()}
renderItem={({item}) => <VoyanteItem voyante={item} displayDetailForVoyante={this._displayDetailForVoyante} />}
numColumns={numColumns} />
</View>
)
}
}
const styles = StyleSheet.create({
main_container: {
flex: 1,
backgroundColor: '#dfdee1',
backgroundColor: 'white',
flexDirection: 'column',
},
textinput: {
marginLeft: 5,
marginRight: 5,
height: 50,
borderColor: '#000000',
borderWidth: 1,
paddingLeft: 5,
backgroundColor: 'white'
},
button: {
backgroundColor: '#EF3934',
borderColor: '#EF3934',
marginLeft: 20,
marginRight: 20
},
text: {
backgroundColor: '#EF3934',
borderColor: '#EF3934',
marginLeft: 20,
marginRight: 20
},
flatList: {
flex: 1,
flexDirection: 'column',
alignContent: 'flex-start',
//justifyContent: 'flex-start',
//alignItems: 'flex-start'
}
})
export default Accueil
``
I found solution :
In your ChaineYT you have written below line :
this.setState(isLoading=true, isLinked=true);
replace it with :
this.setState({isLoading:true, isLinked:true});
I keep getting this error in my debug console..
This is my main App component code :-
import React, {Component} from 'react';
import {StyleSheet, Text, View, FlatList, Image} from 'react-native';
import Bookitem from "./Bookitem";
const mB = [
{
rank: 1,
title: "Gathering Prey",
author: "John Sanford",
image : "http://du.ec2.nytimes.com.s3.amazonaws.com/prd/books/9780399168796.jpg"
},
{
rank: 2,
title: "Memory Man",
author: "David Baldacci",
image : "http://du.ec2.nytimes.com.s3.amazonaws.com/prd/books/9781455586387.jpg"
}
];
export default class App extends Component {
constructor(props){
super(props);
this.state={data : this.addKeystoBooks(mB)};
}
addKeystoBooks = books => {
return books.map(book => {
return Object.assign(book,{key: book.title})
})
}
_renderitem = ({item}) => {
<Bookitem
coverURL = {item.image}
author = {item.author}
title = {item.key}
/>
}
render() {
return <FlatList
data={this.state.data}
renderItem={this._renderitem} />;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
flexDirection : 'row'
},
input : {
fontFamily: 'Lobster-Regular',
fontSize: 24,
padding: 40,
borderWidth: 2,
textAlign : "center"
}
});
My BookItem Component code is as follows :-
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default class book extends Component {
render(){
return(
<View style = {styles.container}>
<Image style = {styles.cover} source = {this.props.coverURL} resizeMode="contain" />>
<View style = {styles.info}>
<Text style = {styles.author}>{this.props.author}</Text>
<Text style ={styles.title}>{this.props.title}</Text>
</View>
</View>
)
}
}
const styles = StyleSheet.create(
{
container : {
flex:1,
justifyContent: "center",
alignItems: "center",
flexDirection: "row",
backgroundColor:"#FFFFFF",
borderBottomColor:"#AAAAAA",
borderBottomWidth: 2,
padding: 5,
height: 175
},
cover : {
flexDirection:"column",
alignItems: "flex-start",
height: 150
},
info:{
flexDirection: "column",
alignItems: "flex-end",
flex: 3,
alignSelf:"center",
padding: 20
},
author: {
fontFamily: "Lobster-regular",
fontSize: 20
},
title : {
fontSize: 23,
fontFamily: "Lobster-Regular"
}
}
)
I have used addkeystobooks function to add a unique identifier to each book for rendering of the flatList's data
I hope this information is sufficient enough to tell me where i am going wrong, i am trying to work with the react-native based on a mock book set..
I am new to react native. I tried using the createStackNavigator module. However, I do not know why my onClick function is not directing me to the required screen. Here are my codes are shown below:
mySpaceRouter.js
import {createStackNavigator} from 'react-navigation'
import SubscriptionScreen from './subscribed'
import MySpaceScreenRT from './myspace'
import React, {Component} from 'react'
const RootStack = createStackNavigator(
{
MySpace : MySpaceScreenRT,
subscribed : SubscriptionScreen,
navigationOptions:{
header:{ visible:false }
}
},
{
initialRouteName : 'MySpace',
},
)
class MySpaceScreen extends Component{
render(){
return(
<RootStack />
)
}
}
export default MySpaceScreen;
mySpace.js
import React, { Component } from 'react'
import { StyleSheet, Text, View, ScrollView, TouchableOpacity } from 'react-native'
import { Avatar, Button, Icon } from 'react-native-elements'
import MyButton from '../Button'
class MySpaceScreenRT extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<View style={styles.textHolder}>
<Text style={styles.headerText}>My Space</Text>
</View>
</View>
<View style={styles.boxContainer} >
<ScrollView style={styles.scrollContainer}>
<View style={styles.profileContainer}>
<Avatar
large
rounded
title="CR"
onClick={() =>this.props.navigation.navigate('subscribed')}
activeOpacity={0.7}
/>
<Text style={styles.profileName}>Christaino Ronaldo </Text>
</View>
<MyButton text='Subscribed' icon='ios-play' />
<MyButton text='Downloads' icon='ios-folder-open' onPress ={() => console.log('Works!')} />
<MyButton text='History' icon='ios-timer-outline' />
<MyButton text='Rate Our App' icon='ios-star-half' />
</ScrollView>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flexDirection: 'column',
flex: 1,
},
header: {
height: 70,
backgroundColor: '#780c1c',
elevation: 5,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
},
boxContainer: {
flex: 1,
flexDirection: 'column',
},
textHolder: {
},
headerText: {
fontSize: 20,
color: 'white'
},
profileContainer: {
height: 150,
borderColor : '#696969',
borderBottomWidth: 0.5,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'transparent',
},
profileName: {
position: 'relative',
zIndex: 1,
fontSize: 16,
color: '#000000',
alignSelf: 'center',
marginTop: 10,
marginLeft: 10
},
scrollContainer: {
flexDirection: 'column',
},
icons: {
marginTop: 10
},
Text: {
fontSize: 18,
alignSelf: 'center',
padding: 10
}
})
export default MySpaceScreenRT;
subscribed.js
import React, {Component} from 'react'
import {StyleSheet, Text, View} from 'react-native'
class SubscriptionScreen extends Component {
render(){
return(
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>SubscriptionScreen!</Text>
</View>
)
}
}
export default SubscriptionScreen;
Thank you.
With react-native you use onPress() not onClick().
onPress={() =>this.props.navigation.navigate('subscribed')}
For each screen in the stack you have to create an entry so you should do something like this:
const RootStack = createStackNavigator({
MySpace: {
screen: MySpace,
navigationOptions: ({ navigation }) => ({
title: "My Space" ,
header:{ visible:false }
}),
},
subscribed: {
screen: SubscriptionScreen,
navigationOptions: ({ navigation }) => ({
title: "" ,
header:{ visible:false }
}),
}
},{
initialRouteName : 'MySpace',
})
in addition to that you have to change onClick to onPress
I've made CustomHeader like below.
import React from 'react'
import {Platform, View, Text, TouchableHighlight, TouchableOpacity, StyleSheet} from 'react-native'
import {Ionicons} from '#expo/vector-icons';
import ScaleImage from 'react-native-scalable-image';
import {scale, moderateScale, verticalScale} from '../utils/scaling';
export default class CustomHeader extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.wrap}>
<TouchableOpacity style={styles.goBack} onPress={() => {
this.props.nav.goBack(null)
}}>
<Ionicons name="ios-arrow-back" size={moderateScale(33)} color="#565656"/>
</TouchableOpacity>
<View style={styles.title}>
<Text style={styles.titleText}>Title</Text>
</View>
<TouchableOpacity style={styles.share}>
<Ionicons name="ios-share-outline" size={moderateScale(30)} color="#565656"/>
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: Platform.OS === 'ios' ? Expo.Constants.statusBarHeight : 0
},
wrap: {
height: moderateScale(50),
flexDirection: 'row',
borderBottomWidth: 1,
borderBottomColor: '#e5e5e5'
},
goBack: {
flex: 1,
alignItems: 'flex-start',
justifyContent: 'center',
paddingLeft: 12,
},
title: {
flex: 8,
alignItems: 'center',
justifyContent: 'center'
},
titleText: {
fontSize: moderateScale(15),
fontWeight: 'bold'
},
share: {
flex: 1,
justifyContent: 'center',
paddingRight: 12,
alignItems: 'flex-end',
}
});
And this component is imported ContentDetailScreen.js below.
import React from 'react';
import CustomHeader from '../components/CustomHeader'
import {Platform, View, ScrollView, StyleSheet, Text, Dimensions, Image, Button, Modal} from 'react-native';
import ScaleImage from 'react-native-scalable-image';
import {scale, moderateScale, verticalScale} from '../utils/scaling';
import {Ionicons} from '#expo/vector-icons';
import Swiper from 'react-native-swiper'
const {width} = Dimensions.get('window')
export default class ContentDetailScreen extends React.Component {
static navigationOptions = {
header: null
};
render() {
console.log(this.props.navigation);
return (
<View style={[styles.container]}>
<CustomHeader nav={this.props.navigation}/>
<Text style={{backgroundColor: 'red'}}>Something</Text>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
shareIcon: {
color: '#565656'
},
wrapper: {
backgroundColor: '#333',
justifyContent: 'flex-start'
},
slide: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#111'
},
text: {
color: '#fff',
fontSize: 30,
fontWeight: 'bold'
},
image: {
width,
flex: 1
},
paginationStyle: {
position: 'absolute',
bottom: 10,
right: 10
},
paginationText: {
color: '#333',
fontSize: 20
}
});
But, It's displaying like .
I would like to be on just next of CustomHeader as common.
But 'Something (red color)' is on bottom of Screen.
What's wrong?
If I remove CustomHeader, It's correct.
remove the
flex: 1,
from the CustomHeader style, and you will get what you want