react native scrollview showing up on down part of the emulator - reactjs

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,
},

Related

Unable to figure out the source of the error - react native

I am developing react-native app for quite sometimes. During my development, almost everyday i am facing some error/warning. Among them, the most comment error I've faced is this-> Warning: Can't perform a React state update on an unmounted component. I've searched everywhere, but couldn't find a proper solution. And this log also not explaining that much. So is there anyone who can explain this which will be much more understandable, or point me out where should I dig into to solve this error or what more should I study to understand the situation. Here is the full screenshot of this error.
. And Here is the some code of one of my component:
//packages
import React, { useContext, useEffect, useState } from 'react';
import { ActivityIndicator, ImageBackground, Pressable, StyleSheet, Text, View } from 'react-native';
// third pirty packages
import { Button, HamburgerIcon, Menu, NativeBaseProvider, useToast } from 'native-base';
import Feather from 'react-native-vector-icons/Feather';
import Foundation from "react-native-vector-icons/Foundation";
import NetInfo from "#react-native-community/netinfo";
import {
widthPercentageToDP as wp,
heightPercentageToDP as hp,
listenOrientationChange as lor,
removeOrientationListener as rol
} from 'react-native-responsive-screen';
//assets and components
import { AuthContext } from './../context';
const MainBody = (props) => {
const { signOut } = useContext(AuthContext);
const [isLoading, setIsLoading] = useState(false);
const toast = useToast();
useEffect(() => {
lor();
return () => {
rol();
};
}, []);
const styles = StyleSheet.create({
wrapperView: {
height: hp('87%'),
paddingTop: hp('7%'),
alignItems: 'center',
backgroundColor: '#fff'
},
dashboardView: {
width: '80%',
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
marginBottom: hp('3%')
},
dashboardCategory: {
width: '45%',
height: hp('20%'),
borderRadius: 5,
elevation: 5,
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
backgroundColor: '#FFFFFF'
},
iconStyle: {
color: 'grey',
fontSize: hp('5%'),
alignSelf: 'center'
},
buttonText: {
marginTop: 10,
color: '#4b2e80',
width: '100%',
textAlign: 'center',
fontSize: hp('2.7%')
},
headerButtonView: {
position: 'absolute',
top: hp('3%'),
right: wp('5%')
}
});
return (
<View>
<View style={styles.wrapperView}>
<View style={styles.dashboardView}>
<Button light style={styles.dashboardCategory}>
<Feather style={styles.iconStyle} name="users" />
<Text style={styles.buttonText}> Clip </Text>
</Button>
<Button light style={styles.dashboardCategory}>
<Foundation style={styles.iconStyle} name='pound' />
<Text style={styles.buttonText}> Balancing </Text>
</Button>
</View>
</View>
<View style={styles.headerButtonView}>
<Menu
trigger={(triggerProps) => {
return (
<Pressable accessibilityLabel="More options menu" {...triggerProps}>
<HamburgerIcon color="#fff" />
</Pressable>
)
}}
>
<Menu.Item onPress={() => signOut()}>Logout</Menu.Item>
</Menu>
</View>
</View>
);
}
export const DashboardScreen = ({ navigation }) => {
return (
<NativeBaseProvider>
<MainBody navigate={navigation.navigate} />
</NativeBaseProvider>
);
}
we need to unsubscribe the particular subscription before our components unmounts. it's a workaround but will get rid of the error.
useEffect(() => {
let mounted = true
if(mounted){
lor();
}
return () => {
rol();
mounted= false
};
}, []);

How to have header/footer stay in place while the content is scrolled?

How to make the Header and Footer stay in place while the content (in the middle of screen is being scrolled)?
I have tried to do it using 'flex' property with no avail thus far.
The example code:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { FlatList } from "react-native-web";
export default function App() {
const items = [];
for (let i = 1; i <= 200; i++) {
items.push(`Item number ${i}`);
}
return (
<View style={styles.container}>
<View>
<Text>HEADER VALUE</Text>
</View>
<FlatList
data={items}
keyExtractor={item => item}
renderItem={({item}) => <Text>{item}</Text>}/>
<View>
<Text>FOOTER VALUE</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Expo Snack of this code:https://snack.expo.io/NAoplIFmA
The current behavior is that the header is gets scrolled away as if the entire screen was surrounded with the scroll view. But the screen is surrounded with regular view so I would have expected the FlatList to be scrollable while the Header stays in place.
What is the proper way to make the header/footer stay in place while the contents of FlatList become scrollable?
You can use the position: "fixed"
Here you go, this should solve it.. Make changes according to your needs.
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { FlatList } from "react-native-web";
export default function App() {
const items = [];
for (let i = 1; i <= 200; i++) {
items.push(`Item number ${i}`);
}
return (
<View style={styles.container}>
<View style = {styles.header}>
<Text style = {{textAlign:'center'}}>HEADER VALUE</Text>
</View>
<FlatList
data={items}
keyExtractor={item => item}
renderItem={({item}) => <Text>{item}</Text>}/>
<View style = {styles.footer}>
<Text>FOOTER VALUE</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
header:
{
position:"fixed",
top:0,
left:0,
alignItems:'center',
justifyContent:'center',
width:'100%',
backgroundColor:'blue',
height:'50px'
},
footer:
{
position:"fixed",
bottom:0,
left:0,
alignItems:'center',
justifyContent:'center',
width:'100%',
backgroundColor:'blue',
height:'50px'
},
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
marginTop:'50px'
},
});

Why is WebView in react native not opening?

using #react-native-community/react-native-webview package (v^8.1.2) to open a webview in a RN v0.61.5 project, no matter what I do, I cannot get the webview to display once the button is pressed. I have a button that opens it, but nothing happens. none of the error functions from the props executed, nothing.
here's the setup:
<Components.Button
style={{ marginLeft: 15 }}
text={"It's Copied. Continue"}
type={'primary'}
onPress={() => (
<WebView
style={{ flex: 0, height: height, width: width }}
containerStyle={{ flex: 0, height: height, width: width }}
automaticallyAdjustContentInsets={true}
ref={(ref) => (this.webview = ref)}
source={{ uri: 'https://connect.stripe.com/oauth/authorize?response_type=code&client_id=<....>&scope=read_write' }}
originWhitelist={['https://*']}
renderError={(error) => <View style={{ flex: 1 }}><Text>{error}</Text></View>}
onError={syntheticEvent => {
const { nativeEvent } = syntheticEvent;
console.warn('WebView error: ', nativeEvent);
}}
onNavigatorStateChange={(event) => {
if (event.url !== this.props.stripeUri) {
this.webview.stopLoading();
Linking.openURL(event.url);
}
}}
/>
)}
/>
As you can see, the things ive tried:
setting flex:0 with a specified height, width
whitelisting uri with wildcard
setting various error props
re-running pod install
No errors register in console, no new error view renders.
the last time I looked at this code it was working fine, not sure what happened. Any thoughts?
EDIT: link to snack: https://snack.expo.io/uTkqnGbny
here is my snack code
https://snack.expo.io/sfDcMtiIR
Code:
import * as React from "react";
import {
Text,
View,
StyleSheet,
TouchableOpacity,
Linking,
Dimensions,
} from "react-native";
import { WebView } from "react-native-webview";
import Constants from "expo-constants";
const { height, width } = Dimensions.get("window");
const testURI = "https://google.com";
export default function App() {
const [isShowWebView, setIsShowWebView] = React.useState(false);
return (
<View style={styles.container}>
<TouchableOpacity
style={{
height: 50,
width: "100%",
borderRadius: 50,
justifyContent:"center",
alignItems:"center"
}}
onPress={() => setIsShowWebView(!isShowWebView)} >
<Text>Open Webview</Text>
</TouchableOpacity>
{isShowWebView && (
<WebView
style={{ height: height, width: width }}
containerStyle={{ height: height, width: width }}
ref={(ref) => (this.webview = ref)}
source={{ uri: testURI }}
renderError={(error) => (
<View style={{ flex: 1 }}>
<Text>{error}</Text>
</View>
)}
onError={(syntheticEvent) => {
const { nativeEvent } = syntheticEvent;
console.warn("WebView error: ", nativeEvent);
}}
onNavigatorStateChange={(event) => {
if (event.url !== testURI) {
this.webview.stopLoading();
Linking.openURL(event.url);
}
}}
/>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: Constants.statusBarHeight,
backgroundColor: "#ecf0f1",
padding: 8,
},
});
I think i know what you want.
I createfd a state var to we toggle the web view using the button.
Please try my snack (using Android or iOS).
https://snack.expo.io/lYItc9ACk
Code:
import * as React from 'react';
import { Text, View, StyleSheet, TouchableOpacity, Linking, Dimensions } from 'react-native';
import { WebView } from 'react-native-webview'
import Constants from 'expo-constants';
const {height, width} = Dimensions.get('window');
const testURI = "https://google.com";
export default function App() {
const [showWebView, setShowWebview] = React.useState(false);
return (
<View style={styles.container}>
<TouchableOpacity
style={{width:'90%', height: 50, backgroundColor:'#333', borderRadius: 50, alignSelf: 'center'}}
onPress={() => setShowWebview(!showWebView)}
>
</TouchableOpacity>
{ !!showWebView &&
<WebView
style={{ flex: 1, height: height, width: width }}
containerStyle={{ flex: 1, height: height, width: width }}
ref={(ref) => (this.webview = ref)}
source={{ uri: testURI }}
renderError={(error) => (
<View style={{ flex: 1 }}><Text>{error}</Text></View>
)}
onError={syntheticEvent => {
const { nativeEvent } = syntheticEvent;
console.warn('WebView error: ', nativeEvent);
}}
onNavigatorStateChange={(event) => {
if (event.url !== testURI) {
this.webview.stopLoading();
Linking.openURL(event.url);
}
}}
/>
}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
});
Remove the entire styling and just go with source: uri

Make the Header scroll down with FlatList and Animated - React Native

I didn't find any soulotion to do animated with FlatList,
I want to hide my Header when I scroll down like In Facebook app.
I tried To use FlatList with diffClamp() without success,
I don't know if I can do it with FlatList but I need also LazyLoading,
Someone Can help me?
This is my Header:
import React, { useState } from "react";
import {
View,
Animated,
Text,
Dimensions,
TouchableOpacity
} from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { Ionicons } from "#expo/vector-icons";
const Header = props => {
const params = props.scene.route.params;
const [headerHeight] = useState(
params !== undefined && params.changingHeight !== undefined
? params.changingHeight
: Dimensions.get("window").height * 0.065
);
return (
<SafeAreaView style={{ backgroundColor: "rgb(152,53,349)" }}>
<Animated.View
style={{
width: Dimensions.get("window").width,
height: headerHeight,
flexDirection: "row",
backgroundColor: "rgb(152,53,349)"
}}
>
<TouchableOpacity
onPress={() => {
props.navigation.openDrawer();
}}
>
<View
style={{
paddingVertical: "15%",
justifyContent: "center",
paddingHorizontal: 25
}}
>
<Ionicons name="ios-menu" size={30} color={"white"} />
</View>
</TouchableOpacity>
<View style={{ justifyContent: "center", marginLeft: "23%" }}>
<Text
style={{
fontSize: 20,
fontWeight: "bold",
textAlign: "center",
color: "white"
}}
>
MyGameenter code here{" "}
</Text>
</View>
</Animated.View>
</SafeAreaView>
);
};
export default Header;
This is my FlatLIst:
import React from "react";
import { View, FlatList, StyleSheet } from "react-native";
import { EVENTS } from "../data/dummy-data";
import Event from "./Event";
const renderGridItem = itemData => {
return <Event item={itemData.item} />;
};
const ShowEvents = props => {
return (
<View style={styles.list}>
<FlatList
keyExtractor={(item, index) => item.id}
data={EVENTS}
renderItem={renderGridItem}
numColumns={1}
/>
</View>
);
};
const styles = StyleSheet.create({
list: {
flex: 1,
justifyContent: "center",
alignItems: "center"
}
});
export default ShowEvents;
Use
onScroll={(e) => console.log(e.nativeEvent.contentOffset.y)}
Working Example: https://snack.expo.io/#msbot01/privileged-candies
import React, { Component } from 'react';
import { Text, View, StyleSheet, ScrollView, FlatList } from 'react-native';
import Constants from 'expo-constants';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
DATA: [],
previous: 0,
hide: false,
};
}
componentDidMount() {
var array = [];
for (var i = 0; i < 100; i++) {
var a = { id: i, value: i };
array.push(a);
}
this.setData(array);
}
setData(a) {
this.setState({
DATA: a,
});
}
Item({ title }) {
return (
<View
style={{
width: '100%',
height: 30,
marginTop: 5,
backgroundColor: 'green',
justifyContent: 'center',
alignItems: 'center',
}}>
<Text />
</View>
);
}
_onScroll(event) {
// console.log('>>>>>>>>>>>'+this.state.data);
if (this.state.previous < event) {
this.setState({
hide: true,
previous: event,
});
} else {
this.setState({
hide: false,
previous: event,
});
}
console.log(event);
}
render() {
return (
<View style={{ flex: 1 }}>
{this.state.hide == true ? null : (
<View
style={{
width: '100%',
height: 50,
justifyContent: 'center',
alignItems: 'center',
}}>
<Text>Hide me while scrolling</Text>
</View>
)}
<FlatList
onScroll={e => this._onScroll(e.nativeEvent.contentOffset.y)}
data={this.state.DATA}
renderItem={({ item }) => (
<View
style={{
width: '100%',
height: 30,
marginTop: 5,
backgroundColor: 'green',
justifyContent: 'center',
alignItems: 'center',
}}>
<Text />
</View>
)}
keyExtractor={item => item.id}
/>
</View>
);
}
}
const styles = StyleSheet.create({});
import React, { useState } from "react";
import { View, FlatList, StyleSheet, Platform, Dimensions } from "react-native";
import { EVENTS } from "../data/dummy-data";
import Event from "./Event";
const HeaderHeight = () => {
if (Platform.OS === "android" && Dimensions.get("window").height < 600)
return Dimensions.get("window").height * 0.075 + 20;
else if (Platform.OS === "android")
return Dimensions.get("window").height * 0.058 + 20;
else return Dimensions.get("window").height * 0.01 + 20;
};
const renderGridItem = itemData => {
return <Event item={itemData.item} />;
};
const ShowEvents = props => {
const [previous, SetPrevious] = useState(false);
const [hide, SetHide] = useState(false);
_onScroll = event => {
const headerHeight = HeaderHeight();
if (event > headerHeight) {
SetHide(true);
props.navigation.setParams({
hide: true
});
SetPrevious(event);
} else if (event < 0.1) {
SetHide(false);
props.navigation.setParams({
hide: false
});
SetPrevious(event);
}
};
return (
<View style={styles.list}>
<FlatList
onScroll={e => _onScroll(e.nativeEvent.contentOffset.y)}
keyExtractor={(item, index) => item.id}
data={EVENTS}
renderItem={renderGridItem}
numColumns={1}
/>
</View>
);
};
const styles = StyleSheet.create({
list: {
flex: 1,
justifyContent: "center",
alignItems: "center"
}
});
export default ShowEvents;

Vertical button using React Native

I am new to react. I want to create a vertical button using react native. How can I do that? The required image of button is attached for your reference. Any help would be really appreciated.
Here is a simple way to do it using Switch:
(First picture: IOS, Second picture: Android)
import React, { Component } from 'react';
import { Text, View, StyleSheet, Switch } from 'react-native';
import { Constants } from 'expo';
export default class App extends Component {
state = {
accept: false
}
render() {
return (
<View style={styles.container}>
<View style={{flex: 2, alignItems: "center"}}>
<Text>Turn on the switch when you want to accept work, and turn it off when you dont!</Text>
</View>
<View style={{flex: 1, alignItems: "center"}}>
<Switch
style={styles.verticalSwitch}
value={this.state.accept}
onTintColor="blue"
onValueChange={() => this.setState({accept:!this.state.accept})}
/>
<Text style={{marginTop: 12}}>
{this.state.accept ? "on" : "off"}
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "row",
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
paddingHorizontal: 20,
},
verticalSwitch: {
transform: [{ rotate: '-90deg'}]
}
});

Resources