Unable to type in a react native text field - react-native

I m trying to create a basic login page but I'm not able to write anything in text field. Here is a file login.js. How can I solve this? I'm a beginner in react-native.
import React ,{Component} from 'react';
import {View ,Text,ImageBackground,TouchableOpacity,TextInput} from 'react-native';
export default class Login extends Component
{
constructor(props)
{
super(props)
this.state={
username:"",
password:""
}
}
render()
{
return(
<ImageBackground source={require('./background2.png')}
style={{height:'100%', width:'100%'}} >
<View style={{width:'100%',height:'100%',alignSelf:'center'
,justifyContent:'center',alignContent:'center',alignItems:'center'}}>
<TextInput placeholder={"Enter the user Name"}
onChangeText={(value)=>this.setState({username:value})}
style={{height:42,width:'80%',borderBottomWidth:1}}>
</TextInput>
<TextInput placeholder={"Enter the password"}
onChangeText={(value)=>this.setState({password:value})}
style={{height:42,width:'80%',borderBottomWidth:1,marginTop:'5%'}}>
</TextInput>
<View style={{marginTop:'5%',width:'80%'}}>
<TouchableOpacity style={{borderWidth:1,height:42,width:'80%'
,justifyContent:"center",alignItems:'center',borderRadius:40,
backgroundColor:'black',alignSelf:'center',textAlign:'center'}}>
<Text style={{color:'white'}}>Login</Text>
</TouchableOpacity>
</View>
</View>
</ImageBackground>
);
}
}

You should try to adding value props to TextInput component. Just pass the state value that you changing onChangeText event. Something like <TextInput value={this.state.password} onChangeText={(value) => this.setState({password : value})}/>.

Please check the following snack.
https://snack.expo.io/HMDWDe!eL
It is working!!
Following is the code...
import React, { Component } from 'react';
import {
View,
Text,
ImageBackground,
TouchableOpacity,
TextInput,
} from 'react-native';
export default class Login extends Component {
constructor(props) {
super(props);
this.state = {
username: '',
password: '',
};
}
render() {
return (
<ImageBackground
source={require('./background2.jpg')}
style={{ height: '100%', width: '100%' }}>
<View
style={{
width: '100%',
height: '100%',
alignSelf: 'center',
justifyContent: 'center',
alignContent: 'center',
alignItems: 'center',
}}>
<TextInput
placeholder={'Enter the user Name'}
onChangeText={(value) => this.setState({ username: value })}
style={{
height: 42,
width: '80%',
borderBottomWidth: 1,
}}></TextInput>
<TextInput
placeholder={'Enter the password'}
onChangeText={(value) => this.setState({ password: value })}
style={{
height: 42,
width: '80%',
borderBottomWidth: 1,
marginTop: '5%',
}}></TextInput>
<View style={{ marginTop: '5%', width: '80%' }}>
<TouchableOpacity
style={{
borderWidth: 1,
height: 42,
width: '80%',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 40,
backgroundColor: 'black',
alignSelf: 'center',
textAlign: 'center',
}}>
<Text style={{ color: 'white' }}>Login</Text>
</TouchableOpacity>
</View>
</View>
</ImageBackground>
);
}
}

Related

Adding ImageBackground to React Native Login Screen

I found this great code sample/layout -- see below -- for a React Native login screen. All I want to do is to have an ImageBackground as opposed to the current solid background.
When I add ImageBackground to the code, it throws everything off and instead of the image covering the entire screen, everything gets squished in the middle and all alingment gets out of whack. What am I doing wrong here?
Here's the original code with solid background:
import React from 'react';
import { StyleSheet, Text, View, TextInput, TouchableOpacity } from 'react-native';
export default class App extends React.Component {
state={
email:"",
password:""
}
render(){
return (
<View style={styles.container}>
<Text style={styles.logo}>HeyAPP</Text>
<View style={styles.inputView} >
<TextInput
style={styles.inputText}
placeholder="Email..."
placeholderTextColor="#003f5c"
onChangeText={text => this.setState({email:text})}/>
</View>
<View style={styles.inputView} >
<TextInput
secureTextEntry
style={styles.inputText}
placeholder="Password..."
placeholderTextColor="#003f5c"
onChangeText={text => this.setState({password:text})}/>
</View>
<TouchableOpacity>
<Text style={styles.forgot}>Forgot Password?</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.loginBtn}>
<Text style={styles.loginText}>LOGIN</Text>
</TouchableOpacity>
<TouchableOpacity>
<Text style={styles.loginText}>Signup</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#003f5c',
alignItems: 'center',
justifyContent: 'center',
},
logo:{
fontWeight:"bold",
fontSize:50,
color:"#fb5b5a",
marginBottom:40
},
inputView:{
width:"80%",
backgroundColor:"#465881",
borderRadius:25,
height:50,
marginBottom:20,
justifyContent:"center",
padding:20
},
inputText:{
height:50,
color:"white"
},
forgot:{
color:"white",
fontSize:11
},
loginBtn:{
width:"80%",
backgroundColor:"#fb5b5a",
borderRadius:25,
height:50,
alignItems:"center",
justifyContent:"center",
marginTop:40,
marginBottom:10
},
loginText:{
color:"white"
}
});
This produces this nice layout:
And I simply add an image background with the following code which doesn't work at all:
<View style={styles.container}>
<ImageBackground
source={require("../../assets/images/background/teton_snake_dimmed.jpg")}
style={styles.imageBackground}
>
<Text style={styles.logo}>HeyAPP</Text>
<View style={styles.inputView} >
<TextInput
style={styles.inputText}
placeholder="Email..."
placeholderTextColor="#003f5c"
onChangeText={text => this.setState({ email: text })} />
</View>
<View style={styles.inputView}>
<TextInput
secureTextEntry
style={styles.inputText}
placeholder="Password..."
placeholderTextColor="#003f5c"
onChangeText={text => this.setState({ password: text })} />
</View>
<TouchableOpacity>
<Text style={styles.forgot}>Forgot Password?</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.loginBtn}>
<Text style={styles.loginText}>LOGIN</Text>
</TouchableOpacity>
<TouchableOpacity>
<Text style={styles.loginText}>Signup</Text>
</TouchableOpacity>
</ImageBackground>
</View>
And here's the updated styles. The only thing I add is the imageBackground:
container: {
flex: 1,
backgroundColor: '#003f5c',
alignItems: 'center',
justifyContent: 'center',
},
logo: {
fontWeight: "bold",
fontSize: 50,
color: "#fb5b5a",
marginBottom: 40
},
imageBackground: {
flex: 1,
resizeMode: "cover"
},
inputView: {
width: "80%",
backgroundColor: "#465881",
borderRadius: 25,
height: 50,
marginBottom: 20,
justifyContent: "center",
padding: 20
},
inputText: {
backgroundColor: "#465881",
height: 50,
color: "white"
},
forgot: {
color: "white",
fontSize: 11
},
loginBtn: {
width: "80%",
backgroundColor: "#fb5b5a",
borderRadius: 25,
height: 50,
alignItems: "center",
justifyContent: "center",
marginTop: 40,
marginBottom: 10
},
loginText: {
color: "white"
}
});
What am I doing wrong here?
P.S. Here's the original code published by #Alhydra:
https://github.com/Alhydra/React-Native-Login-Screen-Tutorial/blob/master/App.js
Here is an example replace the image with the image you want
snack: https://snack.expo.io/#ashwith00/intelligent-banana
code:
import React from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
Image,
} from 'react-native';
export default class App extends React.Component {
state = {
email: '',
password: '',
};
render() {
return (
<View style={styles.container}>
<Image
style={styles.image}
source={{
uri:
'https://media.gettyimages.com/videos/loopable-color-gradient-background-animation-video-id1182636595?s=640x640',
}}
/>
<View style={styles.subContainer}>
<Text style={styles.logo}>HeyAPP</Text>
<View style={styles.inputView}>
<TextInput
style={styles.inputText}
placeholder="Email..."
placeholderTextColor="#003f5c"
onChangeText={(text) => this.setState({ email: text })}
/>
</View>
<View style={styles.inputView}>
<TextInput
secureTextEntry
style={styles.inputText}
placeholder="Password..."
placeholderTextColor="#003f5c"
onChangeText={(text) => this.setState({ password: text })}
/>
</View>
<TouchableOpacity>
<Text style={styles.forgot}>Forgot Password?</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.loginBtn}>
<Text style={styles.loginText}>LOGIN</Text>
</TouchableOpacity>
<TouchableOpacity>
<Text style={styles.loginText}>Signup</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
image: {
flex: 1,
},
container: {
flex: 1,
backgroundColor: '#003f5c',
},
subContainer: {
...StyleSheet.absoluteFillObject,
alignItems: 'center',
justifyContent: 'center',
},
logo: {
fontWeight: 'bold',
fontSize: 50,
color: '#fb5b5a',
marginBottom: 40,
},
inputView: {
width: '80%',
backgroundColor: '#465881',
borderRadius: 25,
height: 50,
marginBottom: 20,
justifyContent: 'center',
padding: 20,
},
inputText: {
height: 50,
color: 'white',
},
forgot: {
color: 'white',
fontSize: 11,
},
loginBtn: {
width: '80%',
backgroundColor: '#fb5b5a',
borderRadius: 25,
height: 50,
alignItems: 'center',
justifyContent: 'center',
marginTop: 40,
marginBottom: 10,
},
loginText: {
color: 'white',
},
});

React Native error: Super expression must either be null or a function

I get the error when i trie to run this app on my Android Emulator. I changed "Component" to "React.Component" but then i get other Problems.
My versions of React, Node and JS are the newest.
The Problem is that im new and have this Code from a Viedo and he didnt got thet Mistake.
Without React.Component:
With React.Component:
import React from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
StatusBar,
Component,
} from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<StatusBar backgroundColor="#1e90ff" barStyle="light-content" />,
<Text style={styles.login}>Login</Text>,
<TextInput style={styles.input} placeholder="Username" />,
<TextInput
style={styles.input}
placeholder="Password"
secureTextEntry
/>
<View style={styles.btnContainer}>
<TouchableOpacity style={styles.userBtn}>
<text style={styles.btnText}>Login</text>
</TouchableOpacity>
<TouchableOpacity style={styles.userBtn}>
<text style={styles.btnText}>Text</text>
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
backgroundColor: '#1e90ff',
},
login: {
textAlign: 'center',
fontSize: 35,
margin: 10,
color: '#fff',
},
input: {
width: '80%',
backgroundColor: '#fff',
padding: 10,
marginBottom: 10,
height: 30,
},
btnContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
width: '90%',
},
userBtn: {
backgroundColor: '#fff',
padding: 15,
width: '40%',
margin: 10,
},
btnText: {
fontSize: 25,
textAlign: 'center',
color: '#1e90ff',
},
});
Can someone help me?
1. TypeError: Super expression must either be null or a function
To fix this
import React, { Component } from 'react';
Then you can use
export default class App extends Component
2. Invariant Violation: Text strings must be rendered within a component
To fix this remove , & change
<text style={styles.btnText}>Login</text>
to
<Text style={styles.btnText}>Login</Text>
EDIT
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<StatusBar backgroundColor="#1e90ff" barStyle="light-content" />
<Text style={styles.login}>Login</Text>
<TextInput style={styles.input} placeholder="Username" />
<TextInput
style={styles.input}
placeholder="Password"
secureTextEntry
/>
<View style={styles.btnContainer}>
<TouchableOpacity style={styles.userBtn}>
<Text style={styles.btnText}>Login</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.userBtn}>
<Text style={styles.btnText}>Text</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
Hope this helps you. Feel free for doubts.

Touchable Highlight not getting blurry when modal opens in react native

I have a modal which blurs the background view when it opens up.But the background has a Touchable Highlight which is not getting blurred.My code for the background View looks like this:
import React, {Component} from 'react';
import {View, Text, Image, StyleSheet, TouchableHighlight} from 'react-native';
import ModalContent from './modalView';
export default class Customquotesconfirmation extends Component {
constructor(props) {
super(props);
this.state={
modalVisible: false,
phoneNumber: '9018938260',
clearTextInputValue: false
}
this.hideModal=this.hideModal.bind(this);
this.showModal=this.showModal.bind(this);
}
showModal(modalState) {
this.setState({ modalVisible: modalState});
}
hideModal(modalState) {
this.setState({modalVisible: modalState});
}
render(){
return(
<View style={(this.state.modalVisible == true) ? styles.modalStyle : styles.modalCloseStyle }>
<Text style={{textAlign:'center', color: '#097543', fontSize: 28, fontFamily: 'SourceSansPro-Black', marginTop:40, marginLeft:40, marginRight:40}}>Thank you for your interest!</Text>
<Text style={{textAlign: 'center', color: '#666666',fontSize: 18, fontFamily: 'SourceSansPro-Black', marginLeft: 40, marginRight:40, marginTop:20 }}>Tap "Submit" and a lawn care professional will contact you with information about tailored service.</Text>
<Image style={{width: 150, height:150, justifyContent:'center', alignItems:'center', marginLeft:150, marginTop: 40}}
source={require('../../../assets/img/mobile_image.png')}></Image>
<Text style={{marginLeft: 20, marginRight:20, marginTop:20, fontSize:8, color:'#666666'}}>By tapping "Submit" I provide my personal information including phone number and consent to: (1) receive autodialed calls, texts, and prerecorded messages from Trygreen regarding my account, including current and possible future services, customer service and billing; and (2) Trugreen's Privacy Policy and Terms and Conditions(including this arbitration provision).I understand that my consent is not required to purchase TruGreen services and that I may revoke consent for automated communications at any time.</Text>
<TouchableHighlight
onPress={() => {this.setState({ modalVisible: true})}}
underlayColor="#FFFFFF">
<View style={styles.submitButtonStyle}>
<Text style={{color: '#FFFFFF', fontSize:25, textAlign: 'center',marginTop:10, fontStyle:'SourceSansPro-Bold'}}>Submit</Text>
</View>
</TouchableHighlight>
{/* Modal Content */}
{
this.state.modalVisible ?
<ModalContent
modalVisible={this.state.modalVisible}
showModal={this.showModal}
hideModal={this.hideModal}
phoneNumber={this.state.phoneNumber}
/> : null
}
</View>
);
}
}
const styles = StyleSheet.create({
modalStyle: {
backgroundColor: 'rgba(0,0,0,0.5)', height:800
},
modalCloseStyle: {
backgroundColor:'rgba(0,0,0,0)'
},
modalViewStyle:{
flex: 1, flexDirection:'column' , marginTop:200, marginBottom:200, marginLeft:80, marginRight:80, height:220, backgroundColor:'#FFFFFF'
},
submitButtonStyle: {
width:200, height:50, backgroundColor: '#ff9933', marginLeft:120, borderRadius:2, marginTop:20
},
blurButtonOnModalOpen: {
width:200, height:50, marginLeft:120, borderRadius:2, marginTop:20, backgroundColor: 'rgba(0,0,0,0.5)',
}
})
And my modal view code is:
import React, { Component } from 'react';
import {View, Text, TouchableHighlight, Modal, TextInput, StyleSheet} from 'react-native';
export default class ModalPopup extends Component {
constructor(props) {
super(props);
this.state={
modalVisible: this.props.modalVisible,
}
}
render() {
return(
<View style={{flex:1, alignItems:'center', justifyContent:'center'}}>
<Modal
animationType='slide'
visible={this.state.modalVisible}
transparent={true}
onRequestClose={() => {
this.props.hideModal(false);
}
}
>
<View style={ (this.state.modalVisible ? styles.modalViewStyle : null )}>
<View style={{ backgroundColor: '#32CD32', height: 100}}>
<TouchableHighlight
onPress={() => {this.setState({ clearTextInputValue: true})}}
underlayColor="##32CD32">
<Text style={{ marginLeft:10, marginTop:10, color:'#FFFFFF', fontSize: 20}}> x </Text>
</TouchableHighlight>
<Text style={{ marginLeft:60, color:'#FFFFFF', fontSize: 15, marginTop: -25}}> Contact Info </Text>
<Text style={{ marginLeft:40, marginTop:10, color:'#FFFFFF', fontSize: 20, fontStyle: 'SourceSansPro-Bold' }}> Phone Number </Text>
</View>
<View style={{backgroundColor:'#FFFFFF'}}>
<Text style={{ marginLeft:20, marginTop:10, color:'grey', fontSize: 20, fontStyle: 'SourceSansPro-Bold', marginRight:20 }}> Please confirm best contact number. </Text>
<TextInput
style={{fontSize:20, height:40, justifyContent:'center', borderTopWidth: 2, borderLeftWidth: 2, borderColor:'grey', width:200, height: 50, marginLeft: 20, marginTop: 20}}
keyboardType= 'numeric'
defaultValue={this.state.clearTextInputValue ? '' : this.props.phoneNumber}
maxLength={10}></TextInput>
<TouchableHighlight
onPress={() => {this.props.hideModal(false)}}
underlayColor="#FFFFFF">
<View style={{ width:200, height:50, backgroundColor: '#ff9933', marginLeft:20, borderRadius:2, marginTop:20}}>
<Text style={{color:'#FFFFFF', fontSize:25, textAlign: 'center',marginTop:10, fontStyle:'SourceSansPro-Bold'}}>Submit</Text>
</View>
</TouchableHighlight>
</View>
</View>
</Modal>
</View>
);
}
}
const styles = StyleSheet.create({
modalViewStyle:{
flex: 1, flexDirection:'column' , marginTop:200, marginBottom:200, marginLeft:80, marginRight:80, height:220, backgroundColor:'#FFFFFF'
},
submitButtonStyle: {
width:200, height:50, backgroundColor: '#ff9933', marginLeft:120, borderRadius:2, marginTop:20
},
})
Now when the modal opens up, the screen looks like this:
Can someone please help me with this issue.

child component overflow from parent component

import React, {Component} from 'react';
import {Modal, Text, TouchableHighlight, View, Alert} from 'react-native';
export default class AlertModal extends Component {
constructor(props){
super(props)
this.state={
check:'234',
primaryColor:'#dcdcdc',
secondaryColor:'#ff1493',
fontFamily:'sans-serif',
one:'Alert',
two:'sample text'
}
}
state = {
modalVisible: false,
};
setModalVisible(visible) {
this.setState({modalVisible: visible});
}
render() {
return (
<Modal
animationType="slide"
transparent={false}
visible={this.state.modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}>
<View
style={{
top:'39%',
backgroundColor:this.state.primaryColor,
height:'25%',
width:'70%',
alignSelf:'center'
}}>
<Text
style={{
fontWeight:'500',
fontFamily:this.state.fontFamily,
alignSelf:'center',
fontSize:30,
color:this.state.secondaryColor
}}>
{this.state.one}
</Text>
<View
style={{
borderBottomColor: 'black',
borderBottomWidth: 1,
top:'3%'
}}
/>
<Text
style={{
fontFamily:this.state.fontFamily,
alignSelf:'center',
color:this.state.secondaryColor,
top:'100%'
}}>
{this.state.two}
</Text>
</View>
</Modal>
);
}
}
I am trying to create a new Modal, when I try position last text element 'sample two' in modal component within View, I am failing. 'sample two' is displayed outside of view. I will post the screenshot of what I am getting
but I the need the sample text to be within the end of the box, I don't know why it is displayed outside of the box.
Just wrap it inside another view as shown below.
render() {
return (
<Modal
animationType="slide"
transparent={false}
visible={!this.state.modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}>
<View
style={{
top: '39%',
backgroundColor: this.state.primaryColor,
height: '25%',
width: '70%',
alignSelf: 'center',
borderWidth: 1,
borderColor: 'red',
}}>
<Text
style={{
fontWeight: '500',
fontFamily: this.state.fontFamily,
alignSelf: 'center',
fontSize: 30,
color: this.state.secondaryColor,
}}>
{this.state.one}
</Text>
<View
style={{
borderBottomColor: 'black',
borderBottomWidth: 1,
top: '3%',
}}
/>
<View>
<Text
style={{
fontFamily: this.state.fontFamily,
alignSelf: 'center',
color: this.state.secondaryColor,
top: '100%',
}}>
{this.state.two}
</Text>
</View>
</View>
</Modal>
);
}
top: '100%', is moving the Text out, change it to 50%
<Text
style={{
fontFamily: this.state.fontFamily,
alignSelf: 'center',
color: this.state.secondaryColor,
top: '50%',
}}>
{this.state.two}
</Text>

How to implement time picker functionality in react native

I want to implement a functionality that should allow me to pick time within a range.
it should be like this
I have used react-native-slider and my code is here
import React, { Component } from 'react';
import { View, Text, Image, TextInput, StyleSheet } from 'react-native';
import Slider from 'react-native-slider';
import colors from '../styles/colors';
import Strings from '../localization/strings';
class FindDoctor extends Component {
constructor(props) {
super(props);
this.state = {
value: 6,
};
}
getInitialState() {
return {
value: 2,
};
}
render() {
return (
<View style={styles.container}>
<View style={{ flexDirection: 'column', margin: 20 }}>
<Text style={styles.textStyle}>Looking for</Text>
<View style={styles.input}>
<TextInput
placeholder={Strings.InternalMedicine}
autoCorrect={this.props.autoCorrect}
autoCapitalize={this.props.autoCapitalize}
returnKeyType={this.props.returnKeyType}
placeholderTextColor={colors.dimgray}
underlineColorAndroid="transparent"
onChangeText={TextInputName => this.setState({ TextInputName })}
/>
</View>
</View>
<View style={{ flexDirection: 'column', margin: 20 }}>
<Text style={styles.textStyle}>Near</Text>
<View style={styles.input}>
<TextInput
placeholder={Strings.Place}
autoCorrect={this.props.autoCorrect}
autoCapitalize={this.props.autoCapitalize}
returnKeyType={this.props.returnKeyType}
placeholderTextColor={colors.dimgray}
underlineColorAndroid="transparent"
onChangeText={TextInputName => this.setState({ TextInputName })}
/>
</View>
</View>
<View style={{ flexDirection: 'column', margin: 20 }}>
<Text style={styles.textStyle}>Within</Text>
<View style={styles.sliderStyles}>
<Slider
value={this.state.value}
onValueChange={(value) => this.setState({ value })}
minimumValue={4}
maximumValue={10}
step={10}
/>
<Text>value: {this.state.value}</Text>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
textStyle: {
fontSize: 20,
color: colors.brandBlue,
},
input: {
backgroundColor: colors.white,
height: 40,
width: '80%',
marginTop: 5,
borderRadius: 4,
alignItems: 'flex-start',
justifyContent: 'flex-start',
borderWidth: 1,
borderColor: colors.light_gray,
fontSize: 20
},
sliderStyles: {
//margin: 10,
alignItems: 'stretch',
justifyContent: 'center',
},
});
export default FindDoctor;
and now the output look like this
But i want to add multiples values in slider like the image shown above
can anyone help me to add multiple values in slider