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

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.

Related

Nested Texts not aligning (React Native)

I have this basic setup in React Native:
class App extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.textContainer}>
<View>
<Text style={styles.textHighlighting}>
Testing
</Text>
</View>
.
</Text>
<Text style={styles.textContainer}>
<View>
<Text style={styles.textHighlighting}>
Again
</Text>
</View>
!
</Text>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
textContainer: {
fontSize: 50
},
textHighlighting: {
backgroundColor: "yellow",
borderRadius: 10,
fontSize: 50
}
})
I am wrapping the inner text in a view because I want it to be highlighted and to be able to add a borderRadius. This seems to break when I don't have the View. But the text I don't have within the View seems to align itself lower vertically than the highlighted text.
It seems to work fine on IOS. Just not on Android.
Check this expo snack https://snack.expo.dev/#gaurav1995/carefree-truffle
import * as React from 'react';
import { Text, View, StyleSheet } 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 (
<View style={styles.container}>
<View style={styles.textContainer}>
<Text style={styles.textHighlighting}>
Testing
</Text>
<Text style={styles.newFont} >.</Text>
</View>
<View style={styles.textContainer}>
<Text style={styles.textHighlighting}>
Again
</Text>
<Text style={styles.newFont} >!</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
textContainer: {
flexDirection:'row',
marginVertical:5,
padding:2
},
textHighlighting: {
backgroundColor: "yellow",
borderRadius: 10,
fontSize: 50,
},
newFont:{
fontSize:50,
marginLeft:2
}
});
check this out
render() {
return (
<View style={styles.container}>
<Text style={styles.textContainer}>
<Text style={styles.textHighlighting}>Testing</Text>.
</Text>
<Text style={styles.textContainer}>
<Text style={styles.textHighlighting}>Again</Text>!
</Text>
</View>
);
}
}

Unable to type in a react native text field

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>
);
}
}

react-native position element at the bottom

I am trying to move a react native component called "forum" to the bottom of the screen, I tried position: fixed, bottom:0 but it didn't work
I also tried 'flex-end' and still didn't work
any idea what's wrong?
here is the expo code:
import React from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { DrawerNavigator } from 'react-navigation';
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold', marginTop: 20 }}>Home</Text>
</View>
);
}
}
class Enquiry extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold', marginTop: 20 }}>Enquiry</Text>
</View>
);
}
}
class Batches extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold', marginTop: 20 }}>Batches</Text>
</View>
);
}
}
class Forum extends React.Component {
render() {
return (
<View style={{ flex:1,justifyContent:'center',alignItems: 'center',position:'absolute',bottom:0}}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('DrawerOpen')}>
<Text>Open Drawer</Text>
</TouchableOpacity>
<Text style={{ fontWeight: 'bold',marginBottom:36}}>Forum</Text>
</View>
);
}
}
const MyDrawerNavigator = new DrawerNavigator(
{
Home: HomeScreen,
Batches:Batches,
Enquiry:Enquiry,
Forum:Forum,
},
{
drawerBackgroundColor: 'rgba(255,255,255,.9)',
contentOptions: {
activeTintColor: '#fff',
activeBackgroundColor: '#6b52ae',
},
}
);
export default MyDrawerNavigator;
I am not allowed to share images but click the link to see
enter image description here
The valid position option in react native is relative or absolute. The default value is relative.
If you want scroll content and fix position of Forum in your screen, you have to separate your view hierarchy like the following and use absolute position.
<View>
<ScrollView>
...anything content
</ScrollView>
<Forum style={{position:'absolute', bottom: 0}}/>
</View>
<...>
<MyDrawerNavigator .../>
<Forum style={{position:'absolute', bottom: 0}} .../>
<.../>

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.

React Native: Align two TextInputs side by side

I am just starting out with React Native and I am developing an app using RN ... I am bit stuck here ... I have a form in one of the app's component that have couple of TextInputs aligned side by side like in the image below
Here is the code that I have written trying to achieve the above design.
import React, {Component} from 'react';
import {View, Text, StyleSheet, TextInput, TouchableHighlight} from 'react-native';
export default class AddItems extends Component {
onAdd() {
alert('Hello');
}
render() {
return (
<View style={addItemStyles.wrapper}>
<View>
<Text>Item to give cash credit for:</Text>
<View>
<View>
<TextInput placeholder="Test" style={{justifyContent: 'flex-start',}} />
</View>
<View>
<TextInput placeholder="Test" style={{justifyContent: 'flex-end',}} />
</View>
</View>
</View>
</View>
);
}
}
const addItemStyles = StyleSheet.create({
wrapper: {
padding: 10,
backgroundColor: '#FFFFFF'
},
inputLabels: {
fontSize: 16,
color: '#000000',
marginBottom: 7,
},
inputField: {
backgroundColor: '#EEEEEE',
padding: 10,
color: '#505050',
height: 50
},
inputWrapper: {
paddingBottom: 20,
},
saveBtn: {
backgroundColor: '#003E7D',
alignItems: 'center',
padding: 12,
},
saveBtnText: {
color: '#FFFFFF',
fontSize: 18,
}
});
But instead I am getting the view like this.
I know this could be a minor thing but as I said above that I am just starting with React Native so you can consider me as a noob ... Thanks in advance for your help. Looking forward to your answers.
use "flexDirection" in style
render() {
return (
<View style={addItemStyles.wrapper}>
<View>
<Text>Item to give cash credit for:</Text>
<View style={{flexDirection:"row"}}>
<View style={{flex:1}}>
<TextInput placeholder="Test" style={{justifyContent: 'flex-start',}} />
</View>
<View style={{flex:1}}>
<TextInput placeholder="Test" style={{justifyContent: 'flex-end',}} />
</View>
</View>
</View>
</View>
);
}