I'm currently making an fitness app and would like to have a "start session" button on showcase of workout page... I've tried making it absolute and stuff like that, but when I scroll down the page the button is fixed to certain part of the page, But I would like it to go down and stay in the same view whenever I scroll.
This is my code:
import React from 'react';
import {Button, Text, View, StyleSheet, Image, TouchableOpacity} from 'react-native';
const Push_day_showcase = ({navigation}) => {
return(
<View style={styles.container1}>
<Text style={{color: 'white'}}>
This is push-day-showcase page!
</Text>
<TouchableOpacity style={styles.barButton} onPress={()=>navigation.navigate("Push day workout")}>
<Text>Start Session</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container1: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#202020',
},
barButton:{
alignItems: 'center',
padding:5,
paddingLeft: 15,
paddingRight: 15,
margin: 8,
marginBottom: 5,
marginTop: 20,
borderRadius: 10,
backgroundColor: 'red'
},
}
);
export default Push_day_showcase;
This is doable by using flexbox:
import { Text, View, StyleSheet, Button, Image, TouchableOpacity, ScrollView } 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 (
<ScrollView contentContainerStyle={styles.container1}>
<View style={{flex: 1}}>
<Text style={{color: 'white', fontSize: 64}}>
This is push-day-showcase page!
This is push-day-showcase page!
This is push-day-showcase page!
</Text>
</View>
<TouchableOpacity style={styles.barButton}>
<Text>Start Session</Text>
</TouchableOpacity>
</ScrollView>
);
}
const styles = StyleSheet.create({
container1: {
flexGrow: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#202020',
padding: 32
},
barButton:{
alignItems: 'center',
padding:5,
paddingLeft: 15,
paddingRight: 15,
margin: 8,
marginBottom: 5,
marginTop: 20,
borderRadius: 10,
backgroundColor: 'red',
},
});````
Related
So, I was building a simple mobile application on react native. On my history page,
I built a table using the react-native-table-component library, and on pressing the column of the table, it would redirect the user to a page called 'test'.
But whenever I try to run the app, it always shows "TypeError: undefined is not a function (near '...data.map...')"
Can anybody please help?
I have uploaded the code for reference.
HistoryScreen code:
import *as React from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';
import {Table, Row, Rows} from 'react-native-table-component';
import { useState } from 'react/cjs/react.development';
import { NavigationContainer, TabActions} from '#react-navigation/native';
import test from './test';
import HomeScreen from './HomeScreen';
const tableData={
tableHead: ['Incident Report'],
tableData: [
['Incident#1'],
['Incident#2'],
],
};
const styles=StyleSheet.create({
container: {flex: 1, padding: 10, justifyContent:'center', backgroundColor:'#fff'},
head: { height: 44, backgroundColor: 'darkblue' },
headText: { fontSize: 20, fontWeight: 'bold' , textAlign: 'center', color: 'white' },
text: { margin: 6, fontSize: 16, fontWeight: 'bold' , textAlign: 'center' },
});
export default function HistoryScreen({navigation}){
const [data, setData] = useState(tableData);
return(
<View style={styles.container}>
<Table borderStyle={{borderWidth: 4,borderColor: 'teal'}}>
<Rows onPress={()=> navigation.navigate('HomeScreen')} data={data.tableHead} style={styles.head} textStyle={styles.headText}/>
<Rows data={data.tableData} textStyle={styles.text}/>
</Table>
</View>
);
}
homepage code
import * as React from 'react';
import {View, Text} from 'react-native';
export default function test(){
return(
<View style={{flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center'}}>
<Text
style={{flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center'}}> test </Text>
</View>
);
}
import * as React from 'react';
import {View, Text} from 'react-native';
export default function HomeScreen( {navigation} ){
return(
<View style={{flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center'}}>
<Text
onPress={()=> alert('This is the "Home" Screen')}
style={{flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center'}}> test test</Text>
</View>
);
}
The problem most likely comes from your HistoryScreen code. This is the part where the error come from.
<Rows onPress={()=> navigation.navigate('HomeScreen')} data={data.tableHead} style={styles.head} textStyle={styles.headText}/>
If you see carefully in your import there are two different components called Row and Rows. From what I saw in the docs, it seems to take two different data shape. Row takes an array and Rows takes an array within array.
This is what your code supposed to look like:
...
<Table borderStyle={{ borderWidth: 4, borderColor: 'teal' }}>
<Row
onPress={() => navigation.navigate('HomeScreen')}
data={data.tableHead}
style={styles.head}
textStyle={styles.headText}
/>
<Rows data={data.tableData} textStyle={styles.text} />
</Table>
...
I have been trying to update my screen dimensions in react native so that the component resizes on-screen rotation with no vain. I have created my style in a separate file (Styles.js) and imported the styles in my login page like below:
Styles.js
import {
StyleSheet,
Dimensions
} from "react-native";
import {
scale,
ScaledSheet
} from "react-native-size-matters";
const styles = ScaledSheet.create({
scrollViewContainer: {
backgroundColor: '#E20030',
alignItems: 'center',
height: Dimensions.get('window').height,
padding: '50#s'
},
container2: {
alignItems: 'center',
justifyContent: 'center',
width: Dimensions.get('window').width,
flex: 1,
},
loginRect: {
borderRadius: '30#s',
marginTop: '20#s',
width: Dimensions.get('window').width * 0.8,
backgroundColor: 'white',
alignItems: 'center'
},
SectionStyle: {
flexDirection: 'row',
marginTop: 0,
marginLeft: '35#s',
marginRight: '35#s',
borderWidth: '1#s',
borderRadius: '5#s',
borderColor: '#dadae8',
},
});
Login.js
import React, { } from 'react';
import {
Text, View, TextInput, TouchableOpacity,
KeyboardAvoidingView,
} from 'react-native';
import { mystyles, styles } from './Components/styles';
const LoginScreen = () => {
return (
<View style={mystyles.scrollViewContainer}>
<KeyboardAvoidingView behavior="padding" style={{ flex: 100 }}>
<View style={mystyles.scrollViewContainer}>
<View style={mystyles.loginRect}>
<View style={{ flexDirection: 'row' }}>
<View style={{ flex: 1, marginLeft: 45, marginTop: 20 }}>
<Text style={{ color: 'black' }}>Username</Text>
</View>
</View>
<View style={styles.SectionStyle}>
<TextInput
style={styles.input}
/>
</View>
<View style={{ flexDirection: 'row' }}>
<View style={{ flex: 1, marginLeft: 45, marginTop: 20 }}>
<Text style={{ color: 'black' }}>Password</Text>
</View>
</View>
<View style={styles.SectionStyle}>
<TextInput
style={styles.input}
/>
</View>
<TouchableOpacity>
<Text style={styles.buttonTextStyle}>LOGIN</Text>
</TouchableOpacity>
</View>
</View>
</KeyboardAvoidingView>
</View>
)
}
export default LoginScreen;
So I have tried using Dimensions onchange listener in my Styles.js but I get an error
ERROR Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
I created this function in the Styles.js file
m
function getDimensions() {
const [dimensions, setDimensions] = useState({
window,
screen
});
useEffect(() => {
const subscription = Dimensions.addEventListener(
"change",
({
window,
screen
}) => {
setDimensions({
window,
screen
});
console.log(dimensions.window.height + " +++ " + dimensions.screen)
}
);
// return () => subscription?.remove();
});
return dimensions.window;
}
Is there any way I can update the component sizes once the screen has been rotated?
This is working for me to detect rotation or changes in the size of the screen (split mode in tablets for example) without eventListeners.
import { Dimensions } from "react-native";
...
useEffect(() => {
...
}, [Dimensions.get("screen").height, Dimensions.get("screen").width]);
Hope this help to anybody
Problem:
I have created a react native component with three touchable opacity components. It is showing like this.
This is how my code is looking.
/* eslint-disable prettier/prettier */
import React, {Component} from 'react';
import {View, Text, TouchableOpacity} from 'react-native';
import LangaugeCard from './LanguageCard';
import styles from '_styles/homescreen';
class Localization extends Component {
render() {
return (
<View style={styles.localization_container}>
<TouchableOpacity>
<LangaugeCard language="Hindi" />
</TouchableOpacity>
<TouchableOpacity>
<LangaugeCard language="English" />
</TouchableOpacity>
<TouchableOpacity>
<LangaugeCard language="French" />
</TouchableOpacity>
</View>
);
}
}
export default Localization;
My LanguageCard looks like this.
/* eslint-disable prettier/prettier */
import React from 'react';
import {View, Text} from 'react-native';
import styles from '_styles/homescreen';
const LanguageCard = (props) => {
const {language, instruction} = props;
return (
<View style={styles.langauge_card}>
<View style={styles.langauge_view}>
<Text style={styles.language}>{language}</Text>
</View>
<View style={styles.instruction_view}>
<Text style={styles.instruction}>{instruction}</Text>
</View>
</View>
);
};
export default LanguageCard;
This is my styling file.
/* eslint-disable prettier/prettier */
import {StyleSheet} from 'react-native';
const styles = StyleSheet.create({
container: {
flexDirection: 'column',
flex: 1,
backgroundColor: '#eeeeee',
height: '100%',
justifyContent: 'center',
},
localization_container: {
flex: 1,
},
langauge_card: {
backgroundColor: '#ffffff',
height: '46%',
marginLeft: '5%',
marginRight: '5%',
borderRadius:35,
flexDirection:'row',
elevation: 1,
},
langauge_view:{
backgroundColor:'#007aff',
marginTop: '8%',
marginBottom: '8%',
marginLeft:'8%',
borderRadius: 15,
width:'30%',
justifyContent: 'center',
},
language:{
fontFamily:'IskoolaPota',
fontSize:24,
textAlign:'center',
color:'#ffffff',
justifyContent:'center',
},
instruction_view:{
marginTop: '8%',
marginBottom: '8%',
marginLeft:'5%',
justifyContent: 'center',
},
instruction:{
color:'#444444',
fontSize: 15,
},
});
export default styles;
What I want to do is to reduce the space between touchable components and put all three buttons center of the screen I tried a lot to find a way to do so but It was unable to do so. Can someone help me to achieve this one by modifying the code? Thank you
I don't think its a good idea to use percentages as height in this case. Right now, your TouchableOpacity is going off the screen because you have height: '46%', Since you have 3 of those, its height is 138% + padding and margins.
I prefer to use flex and use justifyContent for aligning horizontally and align-items for aligning vertically
So in your case,
langauge_card:{
flex: 1,
}
langauge_view:{
flex: 1,
alignItems: "center",
justifyContent: "center"
}
instruction_view:{
flex: 1,
alignItems: "center",
justifyContent: "center"
}
You can play around wth flex, margin, and "flex-start", "center", "flex-end" for justify content and alignItem.
Hope this helped a bit.
Put these styles in localization_container
{
flex: 1,
alignItems: 'center',
JustifyContent: 'center'
}
These styles will make sure items inside the child's view will be shown in the center of the screen then remove the hight of your containers (if you have any) one by one, Try to avoid usage of % as this can unpredictable.
I am new to react native and am struggling to round the corners to text elements, as you can see the corners are still sticking out of the rounded border, and the same happens when I wrap the text in a view element, I don't know how to fix this. Here is the code:
import React from 'react';
import { StyleSheet, Text, View, Button, Alert, Touchable, TouchableHighlight} from 'react-native';
import { render } from 'react-dom';
export default class App extends React.Component {
myButtonPressed(){
Alert.alert("Logout");
}
render() {
return (
<View style={styles.container}>
<TouchableHighlight
onPress={this.myButtonPressed}
>
<Text style={styles.text}> Login </Text>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
text: {
fontSize: 30,
backgroundColor: "#BB2CD9",
paddingVertical: 10,
paddingHorizontal: 40,
color: "#FFFFFF",
borderRadius: 10,
borderWidth: 2,
borderColor: "#FFFFFF"
}
});
What you can do is wrap the text inside view component and pass all neccessary styling to the view.
export default class App extends React.Component {
myButtonPressed(){
Alert.alert("Logout");
}
render() {
return (
<View style={styles.container}>
<TouchableHighlight
onPress={this.myButtonPressed}
>
<View style={styles.btnContainer}>
<Text style={styles.text}> Login </Text>
</View>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
btnContainer: {
backgroundColor: "#BB2CD9",
paddingVertical: 10,
paddingHorizontal: 40,
borderRadius: 10,
borderWidth: 2,
borderColor: "#FFFFFF"
},
text: {
fontSize: 30,
color: "#FFFFFF",
}
});
You can use TouchableOpacity for better experience. You don't need to define border n all with TouchableOpacity.
Try this - Live Demo
export default class App extends React.Component {
myButtonPressed(){
Alert.alert("Logout");
}
render() {
return (
<View style={styles.container}>
<TouchableOpacity
onPress={this.myButtonPressed}
style={styles.hLight}
>
<Text style={styles.text}> Login </Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
hLight: {
fontSize: 30,
backgroundColor: "#BB2CD9",
paddingVertical: 10,
paddingHorizontal: 40,
borderRadius: 10,
borderWidth: 2,
borderColor: "#FFFFFF"
},
text: {
fontSize:20,
color:'white'
}
});
Using TouchableHighlight - Live
export default class App extends React.Component {
myButtonPressed(){
Alert.alert("Logout");
}
render() {
return (
<View style={styles.container}>
<TouchableHighlight
onPress={this.myButtonPressed}
style={styles.hLight}
>
<Text style={styles.text}> Login </Text>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
hLight: {
fontSize: 30,
backgroundColor: "#BB2CD9",
paddingVertical: 10,
paddingHorizontal: 40,
borderRadius: 10,
borderWidth: 2,
borderColor: "#FFFFFF"
},
text: {
fontSize:20,
color:'white'
}
});
I'm making simple To-Do application using React Native, just watching YouTube. The difference video and me is My version is latest RN, and simulating app in Android.
The code looks like same but my TextInput width is not 100%, but very small element as you can see
My code is below, How to fix it?
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
} from 'react-native';
export default class Main extends Component {
render() {
return (
<View style={styles.wrapper}>
<View style={styles.topBar}>
<Text style={styles.title}>
To-Do List
</Text>
</View>
<View style={styles.inputWrapper}>
<TextInput
style={styles.input}/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
wrapper: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'stretch'
},
topBar: {
padding: 16,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#2ecc71'
},
title: {
color: 'white',
fontSize: 20
},
inputWrapper: {
padding: 10,
flexDirection: 'row',
alignItems: 'stretch',
backgroundColor: 'green',
borderColor: 'red'
},
input: {
height: 26,
borderRadius: 8,
backgroundColor: 'white'
}
});
Try removing the flexDirection style from inputWrapper.
See it here.