I have a issue when i try to write react navigation - reactjs

I have begun the React Navigation. I have some issue about my project and i wish everybody can support me.
In the app.js, i code:
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
StatusBar,
} from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
class HomeScreen extends Component {
static navigationOptions = {
header: null
}
render() {
return (
<View style={styles.container}>
<StatusBar backgroundColor="#1e90ff"
barStyle="light-content" />
<Text style={styles.welcome}>Login To My App</Text>
<TextInput
style={styles.input}
placeholder="Username" />
<TextInput
style={styles.input}
placeholder="Password"
secureTextEntry
/>
<View style={styles.btnContainer}>
<TouchableOpacity style={styles.userBtn}
onPress{() => this.props.navigation.navigate('Details')}
>
<Text style={styles.btnTxt}>Login</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.userBtn}
onPress{() => this.props.navigation.navigate('Details')}>
<Text style={styles.btnTxt}>Signup</Text>
</TouchableOpacity>
</View>
</View>
)
}
}
class DetailsScreen extends Component {
static navigationOptions = {
title: 'My App',
headerRight: <View/>
}
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
}
const RootStack = createStackNavigator({
Home: HomeScreen,
Detail: DetailsScreen
},
{
intialRouteName: 'Home',
defaultNavigationOptions: {
headerStyle: {
backgroundColor: "#1e90ff"
}
},
headerTintColor: "#fff",
headerTitleStyle: {
textAlign: "center",
flex: 1,
}
}
);
const AppContainer = createAppContainer(RootStack);
type Props = {};
export default class App extends Component<Props> {
render() {
return (
<AppContainer />
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
backgroundColor: "lightskyblue",
},
welcome: {
fontSize: 30,
textAlign: "center",
margin: 10,
color: "lightgray",
},
input: {
width: "90%",
backgroundColor: "#fff",
padding: 15,
marginBottom: 10,
marginLeft: 20,
},
btnContainer: {
flexDirection: "row",
justifyContent: "space-between",
width: "90%",
marginLeft: 20,
},
userBtn: {
backgroundColor: "#FFD700",
padding: 15,
width: "45%"
},
btnTxt: {
fontSize: 18,
textAlign: "center",
}
})
Detail: I try to write the code based on the youtube https://www.youtube.com/watch?v=bUesHGYxSLg&list=PLQWFhX-gwJblNXe9Fj0WomT0aWKqoDQ-h&index=4&ab_channel=PradipDebnath and i cannot understand the mistake i met.
When i tried to run by npx react-native run-android in terminal, the warning appeared.
Result: Error failed to instal the app. Make sure you have the Android development environmet set up.
Error: Command fail: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081.
Question: How can i fix the mistake? Because when i star a new file, it did not haave the mistake. Thank for your attention.

Related

How to use auto scroll view in react native with dote?

here is my code
App.js
import React from 'react'
import { Image, StyleSheet, TouchableOpacity, Dimensions, Text, View } from 'react-native'
import { scale, verticalScale } from "react-native-size-matters"
import { RFPercentage} from 'react-native-responsive-fontsize';
import { FlatList } from 'react-native';
const dummyData =
[{
title: 'Get instant loans with approvals',
uri: 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTWo3YRChZ3ADpZ7rEfQu1RvBOu9NMWZIMZBaH-a1CArXqx6nLX',//require('../img/1page.jpg'),
id: 1
},
// {
// title: 'Get money in wallet or bank account',
// uri: 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQN0NcV2epN147CiVfr_VAwsbU3VO8rJU0BPphfU9CEsVWa-kRX',//require('../img/1page.jpg'),
// id: 2
// },
// {
// title: 'Refer & earn exciting gifts',
// uri: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSUbS4LNT8rnIRASXO6LGFSle7Mhy2bSLwnOeqDMivYTb2cgTJg',//require('../img/1page.jpg'),
// id: 3
// }
]
const Home = ({ navigation }) => {
const renderItem=({item})=>{
return(
<View>
<Image source={{uri:item.uri}}style={styles.img} />
<View style={styles.dotView}>
<View style={styles.dot}/>
<View style={styles.dot}/>
<View style={styles.dot}/>
</View>
<Text style={styles.text}>{item.title}</Text>
</View>
)
}
return (
<View style={styles.contanier}>
<View style={styles.imgView}>
<Image source={require('../img/homelogo.png')} style={styles.logo} />
</View>
<View style={{marginBottom:8}}>
<FlatList
data={dummyData}
renderItem={renderItem}
keyExtractor={item => item.id}
/>
</View>
<View style={styles.btnview}>
<TouchableOpacity style={styles.btn}
onPress={() => navigation.navigate("PermissionPage")}
>
<Text style={styles.btntext}>Get Started</Text>
</TouchableOpacity>
</View>
</View>
)
}
export default Home;
const styles = StyleSheet.create({
contanier: {
flex: 1,
backgroundColor: '#fff',
},
imgView: {
marginTop: verticalScale(10),
marginLeft: 20,
},
logo: {
width: scale(70),
height: verticalScale(70),
borderRadius: 50,
// position: 'absolute'
},
btnview: {
alignItems: 'center',
justifyContent: 'center',
marginTop:10,
},
btn: {
width: scale(250),
height: verticalScale(55),
borderRadius: 5,
backgroundColor: '#2192FF',
alignItems: 'center',
justifyContent: 'center',
},
btntext: {
fontSize: RFPercentage(3.20),// fontSize: RFValue(17, 680),
color: '#fff',
fontWeight: '600',
fontFamily: 'Roboto'
},
img:{
width:scale(300),
height:verticalScale(450),
},
text:{
fontSize:RFPercentage(3),
fontWeight:'bold',
color:'#000',
textAlign:'center'
},
dotView:{
flexDirection:'row',
justifyContent:'center',
marginBottom:8
},
dot:{
width:scale(8),
height:verticalScale(8),
backgroundColor:'#2192ff',
borderRadius:20,
marginLeft:5
}
})
here is output I want
https://imgur.com/a/yvR5XgM
I'm creating new app in app home page have auto scroll img but I don't know how to use it and do it
I'm fine some lib but i don't use any lib I want without lib.
I'm seen may qustion on internet but I'm not find soution what I want
any one can help me ?

Need Help React Native Image Header Scroll View

I wanted to add a Image Header Scroll View for my Project. I used the below code and which gives me the error
"Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined."
I tried everything i could but i didn't find a solution. Also I totally don't understand why this error is popping.
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
Image,
Dimensions,
StatusBar,
} from 'react-native';
import * as Animatable from 'react-native-animatable';
import {Header} from 'react-navigation-stack';
import HeaderImageScrollView, {
TriggeringView,
} from 'react-native-image-header-scroll-view';
const MIN_HEIGHT = Header.HEIGHT;
const MAX_HEIGHT = 250;
const styles = StyleSheet.create({
title: {
fontSize: 20,
},
section: {
padding: 20,
borderBottomWidth: 1,
borderBottomColor: '#cccccc',
backgroundColor: 'white',
},
sectionTitle: {
fontSize: 18,
fontWeight: 'bold',
},
titleContainer: {
flex: 1,
alignSelf: 'stretch',
justifyContent: 'center',
alignItems: 'center',
},
navTitleView: {
height: MIN_HEIGHT,
justifyContent: 'center',
alignItems: 'center',
paddingTop: 16,
opacity: 0,
},
navTitle: {
color: 'white',
fontSize: 18,
backgroundColor: 'transparent',
},
sectionLarge: {
height: 600,
},
});
class TvShow extends Component {
constructor() {
super();
this.state = {showNavTitle: false};
}
render() {
return (
<View style={{flex: 1}}>
<StatusBar barStyle="light-content" />
<HeaderImageScrollView
maxHeight={MAX_HEIGHT}
minHeight={MIN_HEIGHT}
maxOverlayOpacity={0.6}
minOverlayOpacity={0.3}
fadeOutForeground
renderHeader={() => (
<Text>Hiiii</Text>
)}
renderFixedForeground={() => (
<Animatable.View
style={styles.navTitleView}
ref={navTitleView => {
this.navTitleView = navTitleView;
}}>
<Text style={styles.navTitle}>
Hii
</Text>
</Animatable.View>
)}
renderForeground={() => (
<View style={styles.titleContainer}>
<Text>Hii</Text>
</View>
)}>
<TriggeringView
style={styles.section}
onHide={() => this.navTitleView.fadeInUp(200)}
onDisplay={() => this.navTitleView.fadeOut(100)}>
<Text style={styles.title}>
<Text>Hii</Text>, ( 1998)
</Text>
</TriggeringView>
<View style={styles.section}>
<Text style={styles.sectionTitle}>Overview</Text>
<Text>Hii</Text>
</View>
<View style={[styles.section, styles.sectionLarge]}>
</View>
</HeaderImageScrollView>
</View>
);
}
}
export default TvShow;
it's a library problem, use the version 0.10.3
"react-native-image-header-scroll-view": "0.10.3"

How can we create Side Tabs in React Native

Creating Side Menu in React Application.
This would work both with Expo and react
Create a Navigation Service(Navigation-service.ts)
import { NavigationActions } from 'react-navigation';
function navigate(routeName: any, params?: any) {
NavigationActions.navigate({
routeName,
params
});
setRoute(routeName);
}
export default {
navigate
}
After this
create a new file sidemnu.tsx
import NavigationService from Navigation-service.ts;
import { View, TouchableHighlight, Image } from 'react-native';
openHome() {
NavigationService.navigate('Home');
}
render(){
return (
<View style={[this.props.isLoggedIn ? styles.container : styles.containerHidden]}>
<View style={[this.props.isLoggedIn ? styles.tabContainer : styles.tabContainerHidden]}>
<View>
<TouchableHighlight
style={this.props.currentTab === 'Home' ? styles.activeTab : styles.menuBtnContainer}
onPress={() => this.openHome()}
disabled={!this.props.isLoggedIn}
underlayColor='red'
>
<Image
source='any image url'
/>
</TouchableHighlight>
</View>
</View>
</View>
);
}
Add style
import { StyleSheet } from 'react-native';
const styles = StyleSheet.create({
container: {
width: 108,
backgroundColor: '#002A5C',
borderTopColor: '#002457',
borderWidth: 1
},
containerHidden: {
width: 108,
backgroundColor: '#002A5C'
},
tabContainer: {
flex: 1,
flexDirection: 'column'
},
tabContainerHidden: {
flex: 1,
display: 'none'
},
activeTab: {
backgroundColor: '#008cc3',
height: 108,
width: 108,
padding: 10,
alignSelf: 'center',
justifyContent: 'center'
},
menuBtnContainer: {
height: 80,
width: 80,
alignSelf: 'center',
justifyContent: 'center'
},
});
Call in app.tsx file
<Sidemnu />
Whenever you want to display this side menu.
If you have any queries, please write back.

React Native (android) , components overlapping with shadow issue

I am using a custom component with image and text. The image is relative to the text. Please see the screenshot.
image 1
image 2.
I have used the TouchableOpacity component as root view for that.
In screenshots when long press on the component, two views are overlapping with shadow. It looks ugly when long press.
Please see the code below for reusable component.
'use strict';
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
TouchableOpacity,
Image,
} from 'react-native';
export default class ExampleButton extends Component {
constructor (props) {
super (props);
}
_handleOnPress = () => {
console.log('pressed!!!');
};
render () {
console.log (this.props);
return (
<TouchableOpacity
onPress={this.handleOnPress}
>
<View style={styles.btnCompContainer}>
<View
style={{
backgroundColor: '#FFF',
justifyContent: 'center',
borderRadius: 30,
height: 50,
width: '100%',
}}
>
<Text style={styles.btnTxt}>{this.props.buttonText}</Text>
</View>
<View style={[styles.btnElmContainer]}>
<Image
style={[{height: 30, width: 30,resizeMode:'stretch'}]}
source={this.props.image}
/>
</View>
</View>
</TouchableOpacity>
);
}
}
const styles = StyleSheet.create ({
btnCompContainer: {
flexDirection: 'row',
alignItems: 'center',
height: 60,
marginLeft: 10,
marginRight: 20,
},
btnElmContainer: {
position: 'absolute',
justifyContent: 'center',
alignItems: 'center',
width: 60,
height: 60,
backgroundColor:'#fff',
borderRadius: 30,
shadowColor: '#000000',
shadowOffset: {
width: 1,
height: 1,
},
elevation:5,
shadowRadius: 2,
shadowOpacity: 0.3,
},
btnTxt: {
marginLeft: 80,
color: '#9e9e9e',
fontSize: 16,
fontWeight: 'bold',
textAlign: 'left',
},
});
Here I am using this reusable component in code.
'use strict';
import React, {Component} from 'react';
import {
Animated,
StyleSheet,
Text,
TextInput,
View,
Image,
ImageBackground,
TouchableOpacity,
Button,
StatusBar,
Easing
} from 'react-native';
// Custom Components
const searchBGImg = require('../assets/search-bg-kit.png');
const houseLogo = require('../Resources/house.png');
import ExampleButton from './components/common/example-button';
export default class TypeSelect extends Component<Props> {
// Navigation Option
static navigationOptions = {
header: null
}
constructor() {
super();
this.state = {
visible: false,
x: new Animated.Value(-100),
};
}
slide = () => {
Animated.spring(this.state.x, {
toValue: 0,
easing: Easing.easeOutBack
}).start();
this.setState({
visible: true,
});
};
gotoSearch = () =>{
this.props.navigation.navigate('DocSearch')
}
componentDidMount(){
this.slide();
}
render() {
return (
<View style={styles.container}>
<ImageBackground source={searchBGImg} style={styles.imgBG}>
<View style={{alignItems:'center', marginBottom:20}}>
<Image style={{height:57, width:69, marginBottom:12}} source={houseLogo} />
</View>
<View>
<Animated.View
style={[styles.slideView, {
transform: [
{
translateX: this.state.x
}
]
}]}>
<ExampleButton image={houseLogo} buttonText={'Click here'} />
<ExampleButton image={houseLogo} buttonText={'Click here'} />
</Animated.View>
</View>
</ImageBackground>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
imgBG:{
flex: 1,
flexDirection: "column",
justifyContent:"center",
width: "100%",
height: "100%"
}
})
It is working fine in iOS, but it's not working in android up to the mark. Please help me to work it in android.
Thanks in advance.
You should use renderToHardwareTextureAndroid prop on <View /> component.
In your example like this:
<View style={styles.btnCompContainer} renderToHardwareTextureAndroid >
<View
style={{
backgroundColor: '#FFF',
justifyContent: 'center',
borderRadius: 30,
height: 50,
width: '100%',
}}
>
<Text style={styles.btnTxt}>{this.props.buttonText}</Text>
</View>
<View style={[styles.btnElmContainer]}>
<Image
style={[{height: 30, width: 30,resizeMode:'stretch'}]}
source={this.props.image}
/>
</View>
</View>

conditionality render headerRight - React Native

I have to render headerRight conditionally in navigation options.
Right now
static navigationOptions = ({ navigation }) => ({
title: i18N.t('atmbranchpickHeader'),
headerRight: (
<TouchableHighlight
underlayColor="#E22F39"
onPress={() => {
navigation.navigate("home");
}}
>
<Image
style={{ marginRight: 20 }}
source={require('../../resources/toolbar/home_white.png')}
/>
</TouchableHighlight>
),
headerTintColor: "white",
headerStyle: {
backgroundColor: "#E22F39"
// top: 30
}
});
My Component
import React, { Component } from "react";
import {
View,
TextInput,
Text,
TouchableOpacity,
TouchableHighlight,
StyleSheet,
AsyncStorage,
BackHandler,
Image,
FlatList,
Dimensions,
TouchableWithoutFeedback
} from "react-native";
import i18n from "../../i18n/i18n.js";
import { colors } from "../../constants/colors.js";
import Storage from "../../utils/AsyncStorage.js";
class AtmBranchTypeSelect extends Component {
// Render callBack
constructor(props) {
super(props);
this.state = {
data: [
],
stBool: false,
}
}
async componentWillMount() {
BackHandler.addEventListener('hardwareBackPress', () => this.props.navigation.goBack());
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', () => this.props.navigation.goBack());
}
static navigationOptions = ({ navigation }) => ({
title: i18n.t('atmbranchpickHeader'),
headerRight: (
<TouchableHighlight onPress={() => {
navigation.navigate('home');
}}>
<Image style={{ marginRight: 20 }} source={require('../../resources/toolbar/home_white.png')} />
</TouchableHighlight>),
headerTintColor: 'white',
headerStyle: {
backgroundColor: colors.themeColor,
// top: 30
}
});
_renderList = ({ item }) => {
return (
<TouchableWithoutFeedback onPress={(event) => this._selectedItem(item.key)}>
<View style={styles.listRowContainer}>
<View style={styles.listinside1Container}>
<Image style={styles.listImage} source={item.icon} />
<View style={styles.listContainer} onPress={(event) => this._selectedItem(item.text)} >
<Text style={styles.listHeader} >{item.header}</Text>
<Text style={styles.listValue} >{item.value}</Text>
</View>
</View>
<Image style={styles.listimgArrow} source={require('../../resources/toolbar/chevron_right_grey.png')} />
</View>
</TouchableWithoutFeedback>
);
}
// Render callBack
render() {
return (
<View style={styles.mainWrapper} >
<FlatList data={this.state.data} renderItem={this._renderList} />
</View>
);
}
}
const styles = StyleSheet.create({
mainWrapper: {
flex: 1,
height: Dimensions.get('window').height,
width: Dimensions.get('window').width,
flexDirection: 'column',
justifyContent: 'flex-start'
},
listRowContainer: {
flexDirection: 'row',
marginTop: 10,
height: 80,
backgroundColor: '#FFFFFF',
justifyContent: 'space-between',
borderBottomWidth: 1,
borderColor: 'lightgray'
},
listinside1Container: {
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center'
},
listContainer: {
alignItems: 'flex-start',
justifyContent: 'center',
flexDirection: 'column',
backgroundColor: '#FFFFFF',
// borderBottomWidth: 1,
// borderColor: 'lightgray'
},
listHeader: {
color: 'black',
fontFamily: 'Roboto-Medium',
marginLeft: 10,
fontSize: 18,
},
listValue: {
fontFamily: 'Roboto-Regular',
marginTop: 4,
color: 'black',
marginLeft: 10,
fontSize: 14,
},
listImage: {
alignSelf: 'center',
height: 25,
width: 25,
margin: 10
},
listimgArrow: {
// flex: 1,
// flexDirection:'row',
alignSelf: 'center',
height: 25,
width: 25,
margin: 10
},
listVal: {
borderWidth: 1,
borderRadius: 10,
color: 'darkgreen',
borderColor: 'white',
backgroundColor: 'white',
fontWeight: 'bold'
},
});
export default AtmBranchTypeSelect;
From the code I have, headerRight will be displayed in all scenarios. consider I have a scenario like based on state value I have to enable/disable headerRight Button .
for example this.state.stBool? headerRight:(.....) : null
I have to render in this way.Please guide me to achieve this.
You could nest the navigation options inside the render and toggle it based on the state value. Haven't tested and not positively on performace. Hope it helps.
import React, { Component } from "react";
import {
View,
TextInput,
Text,
TouchableOpacity,
TouchableHighlight,
StyleSheet,
AsyncStorage,
BackHandler,
Image,
FlatList,
Dimensions,
TouchableWithoutFeedback
} from "react-native";
import i18n from "../../i18n/i18n.js";
import { colors } from "../../constants/colors.js";
import Storage from "../../utils/AsyncStorage.js";
class AtmBranchTypeSelect extends Component {
// Render callBack
constructor(props) {
super(props);
this.state = {
data: [],
stBool: false
};
}
async componentWillMount() {
BackHandler.addEventListener("hardwareBackPress", () =>
this.props.navigation.goBack()
);
}
componentWillUnmount() {
BackHandler.removeEventListener("hardwareBackPress", () =>
this.props.navigation.goBack()
);
}
_renderList = ({ item }) => {
return (
<TouchableWithoutFeedback onPress={event => this._selectedItem(item.key)}>
<View style={styles.listRowContainer}>
<View style={styles.listinside1Container}>
<Image style={styles.listImage} source={item.icon} />
<View
style={styles.listContainer}
onPress={event => this._selectedItem(item.text)}
>
<Text style={styles.listHeader}>{item.header}</Text>
<Text style={styles.listValue}>{item.value}</Text>
</View>
</View>
<Image
style={styles.listimgArrow}
source={require("../../resources/toolbar/chevron_right_grey.png")}
/>
</View>
</TouchableWithoutFeedback>
);
};
// Render callBack
render() {
const { stBool } = this.state;
const navigationOptions = ({ navigation }) => ({
title: i18n.t("atmbranchpickHeader"),
headerRight: stBool ? (
<TouchableHighlight
onPress={() => {
navigation.navigate("home");
}}
>
<Image
style={{ marginRight: 20 }}
source={require("../../resources/toolbar/home_white.png")}
/>
</TouchableHighlight>
) : null,
headerTintColor: "white",
headerStyle: {
backgroundColor: colors.themeColor
// top: 30
}
});
return (
<View style={styles.mainWrapper}>
<FlatList data={this.state.data} renderItem={this._renderList} />
</View>
);
}
}

Resources