I implemented a react-native log in page and i added some styles for it. But then i run it on my emiulator, It gives me this reference error can't find variable: style
This is my login.js code --
import React,{Component} from 'react';
import { Alert, Button,Text, TextInput, View,TouchableOpacity, StyleSheet } from 'react-native';
// import { TouchableOpacity } from 'react-native-gesture-handler';
export default class App extends Component {
state = {
email: '',
password: '',
errorMassage:null
}
render() {
return(
<View style={styles.container}>
<Text style={style.greeting}>{"Helloo.."}</Text>
<View style={styles.errorMassage}>
<Text>Error</Text>
</View>
<View style={styles.form}>
<View>
<Text style={styles.inputTitle}>Email Address</Text>
<TextInput style={styles.input} ></TextInput>
</View>
<View>
<Text style={styles.inputTitle}>Password</Text>
<TextInput style={styles.input} securityTextEntry autoCapitalize ="none" ></TextInput>
</View>
</View>
<TouchableOpacity style={styles.button}>
<Text style={{color:"#FFF",fontWeight:"500"}}>Sign in</Text>
</TouchableOpacity>
<TouchableOpacity style={{alignSelf:"center",marginTop:32}}>
<Text style={{color:"#414959",fontSize:13}}>New To EduApp?<Text style={{color:"#E9446A",fontWeight:"500"}}></Text>
</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'red',
},
greeting: {
marginTop:32,
fontSize:18,
fontWeight:"400",
textAlign:"center"
},
errorMassage:{
alignItems:"center"
},
form:{
marginBottom:48,
marginHorizontal:30
},
input:{
borderBottomColor:"#8A8F9E",
borderBottomWidth:StyleSheet.hairlineWidth,
fontSize:15,
color:"#161F3D"
},
inputTitle: {
color:"#8A8F9E",
fontSize:10,
textTransform:"uppercase"
},
button:{
marginHorizontal:30,
backgroundColor:"#E9446A",
borderRadius:4,
alignItems: "center",
justifyContent: "center",
}
});
And this is the error I got in my emulator.
How can I fix this?
The error occurs because of a typo in your code. It is located on line 17 of the code you supplied. style.greetings should be styles.greeting.
Replace
<Text style={style.greeting}>{"Helloo.."}</Text>
with
<Text style={styles.greeting}>{"Helloo.."}</Text>
One way to decrease these kinds of mistakes is to use a code editor that supports the eslint extension which amongs many things underline missing variables. One that I have had good experiences with is Visual Studio Code.
Related
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>
);
}
}
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.
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>
);
}
I have the following code in which I would like the black boxed text to be placed vertically, either centered or aligned to the top, and be cut by the end of the corresponding text view (sample text).
It seems like even though the text is transformed, it still measures its original height and thus shows only the first letter with ...
adding width:50 to the transformed view, will show better results, but the number of lines in the sample text is changing (and also the font sizes).
this is the result I'm getting:
And this is the required result (using paint.net ;)):
Does anyone have an idea how to do it?
Thanks!
/**
* Sample React Native App
* https://github.com/facebook/react-native
* #flow
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View
} from 'react-native';
class SideComponent extends Component{
render(){
return (
<View style={{flex:1, alignItems:'center', justifyContent:'center', borderColor:'black', borderWidth:0.5, width:20,}}>
<View style={{transform: [{ rotate: '90deg'}]}}>
<Text numberOfLines={1} style={{borderColor:'blue'}}>{this.props.text}</Text>
</View>
</View>
)
}
}
class LineComponent extends Component{
render(){
return (
<View style={{flex:1}}>
<Text numberOfLines={2} style={{backgroundColor:'yellow'}}>Sample text</Text>
<Text numberOfLines={2} style={{backgroundColor:'yellow'}}>Sample text</Text>
<Text numberOfLines={2} style={{backgroundColor:'yellow'}}>Sample text</Text>
</View>
)
}
}
export default class App extends Component{
render() {
return (
<View style={styles.container}>
<View style={{flexDirection:'row'}}>
<SideComponent text="Short"/>
<LineComponent/>
</View>
<View style={{flexDirection:'row'}}>
<SideComponent text="LongTextLong" style={{}}/>
<LineComponent/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
flexDirection: 'column',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
I tried something like this for vertical transformation:
<View style={{flexDirection:'row'}}>
<View style={{flex:0.1 }}>
<View style={{left:-130,width:300,height:20,alignItems:'flex-start',justifyContent:'flex-start',transform: [{ rotate: '270deg'}]}}>
<Text numberOfLines={1} style={[AppStyles.regularFontText,{fontSize:12,color:'#8160e5'}]}>Number of Users</Text>
</View>
</View>
<View style={{flex:0.9}}>
<BarChart
style={{height:200,width:width-100[enter image description here][1]}}
data={this.state.line_data}
chartDescription={{ text: '' }}
xAxis={this.state.xAxis}
animation={{durationX: 2000}}
legend={this.state.line_legend}
gridBackgroundColor={processColor('#ffffff')}
drawBarShadow={false}
drawValueAboveBar={true}
drawHighlightArrow={true}
onSelect={this.handleSelect.bind(this)}
highlights={this.state.highlights}
onChange={(event) => console.log(event.nativeEvent)}
/>
</View>
</View>
I am working whit react-native-side-menu, but I have a problem whith the menu that I want to display, in fact when I put the menu property this element is showing all the time also when isOpen is set as false. This is my Code:
import React, {Component} from 'react';
import {Dimensions,ListView,ScrollView,StatusBar,Text,TouchableOpacity,View} from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import {Colors as JoboColors} from '../styles/General';
import SideMenu from 'react-native-side-menu';
//import SideBar from './SideBar';
export default class JobList extends Component {
state = {
isOpen: false,
};
handleMenu = () => {
this.setState({isOpen: !this.state.isOpen});
}
updateMenuState = (isOpen) => {
this.setState({ isOpen});
}
render() {
const myMenu = <UserMenu />;
return (
<SideMenu menu={myMenu}
isOpen={this.state.isOpen}
onChange={this.updateMenuState}>
<View>
<StatusBar
backgroundColor= {JoboColors.orangeC}
barStyle="light-content"
translucent={false}/>
<View style={styles.actionBar}>
<TouchableOpacity style={styles.menuIcon} onPress={this.handleMenu}>
<Icon name="ios-menu" size={32} style={styles.actionBarIcons}/>
</TouchableOpacity>
<Icon name="ios-search" size={30} style={styles.actionBarIcons}/>
</View>
<View style={{backgroundColor: 'white', justifyContent: 'center', flex: 1}}>
<Text>This is the content</Text>
</View>
</View>
</SideMenu>
);
}
}
class UserMenu extends Component {
render() {
return(
<ScrollView style={styles.content}>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
actionBar: {
height: 50,
flexDirection: 'row',
backgroundColor: Colors.orangeA,
alignItems: "center",
padding: 15,
},
actionBarIcons: {
color: "white",
},
menuIcon: {
flex: 2,
},
content: {
backgroundColor: 'blue',
},
});
And this is the view result, please notice that the blue block is the view for the slide-menu. Somebody has a similar problem like this?
Thanks in advance.
Simplified version of answer -
render() {
const myMenu = <UserMenu />;
return (
<View style={{flex:1}}>
<StatusBar backgroundColor="orange" barStyle="light-content" translucent={false}/>
<View style={styles.actionBar}>
<TouchableOpacity style={styles.menuIcon} onPress={this.handleMenu}>
<Text style={styles.actionBarIcons}>menu</Text>
</TouchableOpacity>
<Text style={styles.actionBarIcons}>search</Text>
</View>
<View style={{flex:1}}>
<SideMenu menu={myMenu} isOpen={this.state.isOpen} onChange={this.updateMenuState}>
<View style={{backgroundColor: 'white', justifyContent: 'center', flex: 1}}>
<Text>This is the content</Text>
</View>
</SideMenu>
</View>
</View>
);
}