state disappears when method is call - react-native

I'm working on a class project and my state is disappearing. After componentDidMount console.log(this.state) is fine. I initiate setInterval and call inc(). Somehow when I enter inc() the state gets wiped out.
import React from 'react';
import { TextInput,Button,StyleSheet, Text, View } from 'react-native';
import styles from './styles/styles.js';
debug=true
export default class App extends React.Component {
constructor(){
super()
this.state={timer:'WORK',
workTime: 25*60+0,
breakTime: 5*60+0,
currentTime:0,
remainingTime:null,
min:0,
sec:0,
startFlag:false,
resetFlag:false}
}
componentDidMount(){
this.interval=setInterval(this.inc,10000)
if(debug)console.log('COMPONENTDIDMOUNT',this.state)
}
static getDerivedStateFromProps(nextProps, prevState) {
if(debug)console.log('GETDERIVEDSTATEFROMPROPS',prevState)
return null
}
shouldComponentUpdate(nextProps,nextState){
if(debug)console.log('SHOULDCOMPONENTUPDATE',nextState)
return true
}
componentDidUpdate(){
if(debug)console.log('COMPONENTDIDUPDATE',this.state)
}
componentWillUnmount(){
if(debug)console.log('COMMPONENTWILLUNMOUNT',this.state)
}
startToggle(){
if(endTime === null)this.setState({remainingTime:this.state.workTime,
startFlag:!this.state.startToggle})
else this.setState({remainingTime:!this.state.startFlag})
}
textTime(){
let min = Math.floor(this.state.remainingTime / 60).toString()
let sec = (this.state.remainingTime % 60)
if (sec < 10)sec ? '0' + sec : sec.toString()
this.setState({min:min,sec:sec})
}
inc(){
console.log(this.state)
}
captureInput(){}
render() {
console.log('RENDER',this.state)
return (
<View style={styles.container}>
<Text style={styles.bigFont}>{`${this.state.timer + 'TIMER'}`}</Text>
<Text style={styles.bigFont}>12:00</Text>
<View style={styles.button}>
<Button title='START' onPress={()=>this.startToggle()} />
<Button title='RESET' onPress={()=>this.resetToggle()} />
</View>
<View style={styles.row}>
<Text style={[styles.bold,{marginRight:10},{width:112},
{textAlign:'right'}]}>
'Work Timer:'</Text>
<Text style={styles.bold}> min:</Text>
<TextInput
defaultValue='50'
style={styles.input}
onChangeText={(text) => {this.captureInput(text)}}
/>
<Text style={styles.bold}> sec:</Text>
<TextInput
defaultValue='50'
style={styles.input}
onChangeText={(text) => {this.captureInput(text)}}
/>
</View>
<View style={styles.row}>
<Text style={[styles.bold,{marginRight:10},{width:112},
{textAlign:'right'}]}>
'Break Timer:'</Text>
<Text style={styles.bold}> min:</Text>
<TextInput
defaultValue='50'
style={styles.input}
onChangeText={(text) => {this.captureInput(text)}}
/>
<Text style={styles.bold}> sec:</Text>
<TextInput
defaultValue='50'
style={styles.input}
onChangeText={(text) => {this.captureInput(text)}}
/>
</View>
</View>
)
}
}

You have 2 options:
Change inc() to inc = () =>
or
Change this.inc to this.inc.bind(this)

Change your inc method declaration to
inc = () => {
...
}
As per your code, this inside inc() is not referring to the component, hence you are not getting state either.
Hope this will help!

Related

Navigating back to start screen

I have two different documents with two different screens. After an e-mail validation on the first screen, I'm now able to go to the second screen. However, I want to return to the first screen. None of the mentioned approaches on the React Navigation 5.x documentation works for me.
This is the code on the App.js:
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import firebase from './firebase';
import * as EmailValidator from 'email-validator';
import { HitTestResultTypes } from 'expo/build/AR';
import logo from './assets/Circulo.png';
import AgeInput from './AgeInput';
// Clase que representa el diseño de la pantalla inicial de la app
class HomeScreen extends Component {
state = { username: null, password: null, nonValidInput: null }
_onSubmit = ({ navigation }) =>{
if(EmailValidator.validate(this.state.username) == true) {
this.setState({ nonValidInput: false });
const { username, password } = this.state;
try {
// THIS IS WHERE I GO TO THE SECOND SCREEN
firebase.auth().signInWithEmailAndPassword(this.state.username, this.state.password).then(() => this.props.navigation.navigate('Age'));
} catch {
Alert.alert(
'Error',
'Los datos no son correctos',
[
{ text: 'Ok' }
],
{ cancelable: false }
);
}
} else {
this.setState({ nonValidInput: true });
}
}
render() {
return (
<KeyboardAwareScrollView contentContainerStyle={styles.container} scrollEnabled
enableOnAndroid={true} resetScrollToCoords={{x:0, y:0}}>
<View style={styles.logo}>
<Image source = {logo} style={styles.img}/>
<Text style={styles.textLogoPrimary}>Neuron App</Text>
<Text style={styles.textLogoSecondary}>Test</Text>
</View>
<View style={styles.formElement}>
<Text style={styles.formText}>Correo Electrónico</Text>
<TextInput keyboardType='email-address' placeholder='Email' onChangeText={value => this.setState({ username: value })}
style={styles.formInput} />
{this.state.nonValidInput ? (
<Text style={styles.textAlert}>Correo electrónico no valido.</Text>
) : null}
</View>
<View style={styles.formElement}>
<Text style={styles.formText}>Contraseña</Text>
<TextInput style={styles.formInput} placeholder='Contraseña' onChangeText={value => this.setState({ password: value })}
secureTextEntry={true}/>
</View>
<View style={styles.buttonView}>
<TouchableOpacity style={styles.button} onPress={this._onSubmit}>
<Text style={styles.buttonText}>Iniciar</Text>
</TouchableOpacity>
</View>
</KeyboardAwareScrollView>
);
}
}
const Stack = createStackNavigator();
class App extends Component {
render() {
return (
<NavigationContainer>
<Stack.Navigator screenOptions={{headerShown: false}} initialRouteName="Home">
<Stack.Screen name='Home' component={HomeScreen} />
<Stack.Screen name='Age' component={AgeInput} />
</Stack.Navigator>
</NavigationContainer>
);
}
}
and this is the code on the AgeInput.js
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import Home from './App';
class App extends Component { // AgeInput
state = { date: null, edad: null, day: null, month: null, year: null }
_ageCalc = () => {
if(this.state.day < 32 && this.state.day > 0 && this.state.month < 13 && this.state.month > 0 && this.state.year != 0) {
var fecha = Date.parse(this.state.year + '-' + this.state.month + '-' + this.state.day);
var hoy = new Date();
var fechaNacimiento = new Date(fecha);
var edad_ahora = hoy.getFullYear() - fechaNacimiento.getFullYear();
var mes = hoy.getMonth() - fechaNacimiento.getMonth();
if (mes < 0 || (mes === 0 && hoy.getDate() < fechaNacimiento.getDate())) {
edad_ahora--;
}
this.setState({ edad: edad_ahora });
} else {
Alert.alert(
'Error',
'Por favor introduce una fecha valida',
[
{ text: 'Ok' }
],
{ cancelable: false },
);
}
}
render () {
return (
<KeyboardAwareScrollView contentContainerStyle={styles.container} scrollEnabled enableOnAndroid={true}
resetScrollToCoords={{x:0, y:0}}>
<View style={styles.topView}>
// This is the button I press to go back to the first screen
<TouchableOpacity style={styles.img} onPress={() => this.props.navigator.navigate('Home')}>
<Image source={flecha} />
</TouchableOpacity>
<View style={styles.topTextWrapper}>
<Text style={styles.topTextPrimary}>Bienvenido a Neuron</Text>
<Text style={styles.topTextSecondary}>¿O no?</Text>
</View>
</View>
<View style={styles.middleView}>
<Text style={styles.formText}>Fecha de nacimiento</Text>
<View style={styles.formRow}>
<View style={styles.textInputWrapper}>
<TextInput style={styles.formInput} placeholder='DD' keyboardType='number-pad'
onChangeText={ value => this.setState({ day: value }) }/>
</View>
<View style={styles.textInputWrapper}>
<TextInput style={styles.formInput} placeholder='MM' keyboardType='number-pad'
onChangeText={ value => this.setState({ month: value }) }/>
</View>
<View style={styles.textInputWrapper}>
<TextInput style={styles.formInput} placeholder='AA' keyboardType='number-pad'
onChangeText={ value => this.setState({ year: value }) }/>
</View>
</View>
</View>
<View style={styles.buttonView}>
<TouchableOpacity style={styles.button} onPress={this._ageCalc}>
<Text style={styles.buttonText}>CALCULAR EDAD</Text>
</TouchableOpacity>
</View>
<View style={styles.ageView}>
<Text style={styles.ageTextPrimary}>Tu edad es:</Text>
<Text style={styles.ageNumber}>{this.state.edad}</Text>
<Text style={styles.ageTextSecondary}>Años</Text>
</View>
</KeyboardAwareScrollView>
);
}
}
export default App; // AgeInput
Thanks for your help
You can do something like this...
render () {
const { navigate } = props.navigation;
//function to go to next screen
goToNextScreen = () => {
return navigate('Home');
return (
<KeyboardAwareScrollView contentContainerStyle={styles.container} scrollEnabled enableOnAndroid={true}
resetScrollToCoords={{x:0, y:0}}>
<View style={styles.topView}>
// This is the button I press to go back to the first screen
<TouchableOpacity style={styles.img} onPress={() => this.goToNextScreen()}>
<Image source={flecha} />
</TouchableOpacity>
<View style={styles.topTextWrapper}>
<Text style={styles.topTextPrimary}>Bienvenido a Neuron</Text>
<Text style={styles.topTextSecondary}>¿O no?</Text>
</View>
</View>
<View style={styles.middleView}>
<Text style={styles.formText}>Fecha de nacimiento</Text>
<View style={styles.formRow}>
<View style={styles.textInputWrapper}>
<TextInput style={styles.formInput} placeholder='DD' keyboardType='number-pad'
onChangeText={ value => this.setState({ day: value }) }/>
</View>
<View style={styles.textInputWrapper}>
<TextInput style={styles.formInput} placeholder='MM' keyboardType='number-pad'
onChangeText={ value => this.setState({ month: value }) }/>
</View>
<View style={styles.textInputWrapper}>
<TextInput style={styles.formInput} placeholder='AA' keyboardType='number-pad'
onChangeText={ value => this.setState({ year: value }) }/>
</View>
</View>
</View>
<View style={styles.buttonView}>
<TouchableOpacity style={styles.button} onPress={this._ageCalc}>
<Text style={styles.buttonText}>CALCULAR EDAD</Text>
</TouchableOpacity>
</View>
<View style={styles.ageView}>
<Text style={styles.ageTextPrimary}>Tu edad es:</Text>
<Text style={styles.ageNumber}>{this.state.edad}</Text>
<Text style={styles.ageTextSecondary}>Años</Text>
</View>
</KeyboardAwareScrollView>
);
}
}
export default App;
Just replace this :
// This is the button I press to go back to the first screen
<TouchableOpacity style={styles.img} onPress={() => this.props.navigator.navigate('Home')}>
with
// This is the button I press to go back to the first screen
<TouchableOpacity style={styles.img} onPress={() => this.props.navigation.navigate('Home')}>
Hope it help.s

Invariant Violation: Element type is invalid React-Native

When I try to make reusable components in my code but it shows some error.
Below are my codes
Login.js
render() {
return (
<View style={login.container}>
<Image source={imageLogo} style={login.logo} />
<View style={login.form}>
<ApptiTextInput
value={this.state.email}
onChangeText={this.handleEmailChange}
placeholder={strings.EMAIL_PLACEHOLDER}
/>
<ApptiTextInput
value={this.state.password}
onChangeText={this.handlePasswordChange}
placeholder={strings.PASSWORD_PLACEHOLDER}
/>
<ApptiButton label={strings.LOGIN} handleLoginPress={this.handleLoginPress.bind(this)} />
</View>
</View>
);
}
Apptibutton.js
render() {
const { label, handleLoginPress } = this.props;
console.log(label);
return (
<View style={apptiButton.container}>
<TouchableOptacity onPress={handleLoginPress}>
<Text style={apptiButton.text} >{label}</Text>
</TouchableOptacity>
</View>
);
}
Here is my code
There's a typo in Apptibutton.js, TouchableOpacity instead of TouchableOptacity:
render() {
const { label, handleLoginPress } = this.props;
console.log(label);
return (
<View style={apptiButton.container}>
<TouchableOpacity onPress={handleLoginPress}>
<Text style={apptiButton.text} >{label}</Text>
</TouchableOpacity>
</View>
);
}

React Native - Access Wrapped Component Methods

I'm trying to drive focus to the second field in a form using custom input components. However, I can't seem to access the focus() or other methods of TextInput which I am extending in the custom class. I have seen some information on ref forwarding as well as implementing the focus() function within the class but have not been able to get either working yet.
Whenever I try to hit the "next" button on the keyboard, it says that focus is not a function. Any help or reference would be appreciated.
<View>
<CustomInput
onRef={ref => (this.child = ref)}
autoCapitalize={'none'}
returnKeyType={'next'}
autoCorrect={false}
onSubmitEditing={() => this.lastNameInput.focus()}
updateState={(firstName) => this.setState({firstName})}
blurOnSubmit={false}
/>
<CustomInput
onRef={ref => (this.child = ref)}
autoCapitalize={'none'}
returnKeyType={'done'}
autoCorrect={false}
updateState={(lastName) => this.setState({lastName})}
ref={(input) => { this.lastNameInput = input; }}
onSubmitEditing={(lastName) => this.setState({lastName})}
/>
</View>
export default class UserInput extends Component {
render() {
return (
<View style={styles.inputWrapper}>
<TextInput
style={styles.input}
autoCorrect={this.props.autoCorrect}
autoCapitalize={this.props.autoCapitalize}
returnKeyType={this.props.returnKeyType}
placeholderTextColor="white"
underlineColorAndroid="transparent"
onChangeText={(value) => this.props.updateState(value)}
blurOnSubmit={this.props.blurOnSubmit}
/>
</View>
);
}
}
you need to do some changes in both components. according to https://stackoverflow.com/a/49810837/2083099
import React, { Component } from 'react'
import {View,TextInput} from 'react-native';
class UserInput extends Component {
componentDidMount() {
if (this.props.onRef != null) {
this.props.onRef(this)
}
}
onSubmitEditing() {
if(this.props.onSubmitEditing){
this.props.onSubmitEditing();
}
}
focus() {
this.textInput.focus()
}
render() {
return (
<View style={{ flex: 1 }}>
<TextInput
style={{ height: 100, backgroundColor: 'pink' }}
autoCorrect={this.props.autoCorrect}
autoCapitalize={this.props.autoCapitalize}
returnKeyType={this.props.returnKeyType}
placeholderTextColor="white"
underlineColorAndroid="transparent"
onChangeText={(value) => this.props.updateState(value)}
blurOnSubmit={this.props.blurOnSubmit}
ref={input => this.textInput = input}
onSubmitEditing={this.onSubmitEditing.bind(this)}
/>
</View>
);
}
}
export default class OrderScreen extends Component {
constructor(props) {
super(props);
this.focusNextField = this.focusNextField.bind(this);
this.inputs = {};
}
focusNextField(id) {
this.inputs[id].focus();
}
render() {
return (
<View style={{ flex: 1 }}>
<UserInput
autoCapitalize={'none'}
returnKeyType={'next'}
autoCorrect={false}
updateState={(firstName) => this.setState({ firstName })}
blurOnSubmit={false}
onRef={(ref) => { this.inputs['projectName'] = ref; }}
onSubmitEditing={() => {this.focusNextField('projectDescription');}}
/>
<UserInput
onRef={(ref) => {this.inputs['projectDescription'] = ref}}
autoCapitalize={'none'}
returnKeyType={'done'}
autoCorrect={false}
updateState={(lastName) => this.setState({ lastName })}
/>
</View>
)
}
}

React-Native _this5.setState is not a function when changing the text in TextInput?

I am currently learning React-Native so apologies if the answer has appeared else where. But in my app whenever I change the text in the TextInput the program will crash with the error _this5.setState is not a function
i.e. if I search for 'Hello', everything works fine. But the moment when I change 'Hello' to 'Hell' the program crashes with that error.
Here's my code
export default class App extends React.Component {
constructor(props){
super(props)
this.state = {
apiKey: 'insertMyAPIKey',
isLoading: true,
text: ''
}
this.setInputState = this.setInputState.bind(this)
}
render() {
return (
<View style={{padding: 10}}>
<TextInput
style={{height: 40}}
placeholder="Search For Summoner!"
value={this.state.text}
onChangeText={(text) => this.setState({
text: text
})}
clearButtonMode="while-editing"
/>
<Button
onPress={() => {
this.MyFunctionsToRun(this.state.text)
}}
title="Search"
color="#841584"
/>
<Text style={{padding: 10, fontSize: 20}}>
Searching for summoner: {this.state.text}
</Text>
<Text>
The summoner ID: {this.state.summonerID}
</Text>
<Text>
The summoner Rank: {this.state.leagueName}, {this.state.tier} {this.state.rank}, {this.state.leaguePoints}
</Text>
<Text>
The Summoner Win Rate: {this.state.winRate}
</Text>
<View>
<FlatList
data={this.state.lastTenGames}
renderItem={({item}) => <Text>{item.lane}</Text>}
/>
</View>
</View>
);
}
}
you can try this
onChangeText={text => (this.state.text = text)}
or use another function
onChangeText={this.changeText}
changeText = text => {
this.setState({text: text});
};
i hope it works for you :D

react-native-router-flux. How to change custom navBar programmatically?

I have a custom navBar in my scene:
<Scene key="myPage"
component={MyPage}
navBar={NavBarCustom}
hideNavBar={false}/>
....
class NavBarCustom extends Component {
constructor(props) {
super(props);
}
onExitPressed(){
App.exit();
}
render() {
return (
<View style={styles.navBar}>
<View style={styles.leftContainer}>
<Image
style={styles.logo}
source={require('./../../res/ic_nav_bar.png')}/>
<Text style={[appStyles.customFontBold, styles.title1]}>
MY TITLE
</Text>
</View>
<View style={styles.centralContainer}>
<Text style={[appStyles.customFontRegular, styles.title2]}>
{strings.benefit_identifier}
</Text>
</View>
<View style={styles.rightButtonContainer}>
<TouchableHighlight
style={{padding: 7}}
underlayColor='#b59d6e'
onPress={() => { this.onExitPressed() }}>
<Text style={[appStyles.customFontRegular, styles.rightButtonTitle]}>
{strings.exit}
</Text>
</TouchableHighlight>
</View>
</View>
);
}
}
It works good. So how can I change title1 of NavBarCustom from my scene MyPage?
Thanks in advance.
You can pass/send information through/with props:
In your render you can declare a const which take the react-native-router-flux Actions, set the route which to point and then set the object which to pass:
If there's a Login main file, which then redirects to a Register view then you declare the const goToRegister and then pass the text with its content:
class Login extends Component {
render() {
const goToRegister = () => Actions.Register({text: 'From Login'});
return(
<View style={{flex:1}}>
<Text>
Login
</Text>
<TouchableHighlight onPress={goToRegister} style={{ marginTop: 100}}>
<Text>Go Register</Text>
</TouchableHighlight>
</View>
)
}
}
Then within your Register you receive it just within your props as this.props.text:
class Register extends Component {
render() {
return(
<View style={{flex:1}}>
<TouchableHighlight onPress={Actions.Login} style={{ marginTop: 100}}>
<Text>{ this.props.text }</Text>
</TouchableHighlight>
</View>
)
}
}
In your case you should send firstly the value of your title maybe as:
render() {
const goToMyPage = () => Actions.MyPage({ title: 'Some Title'})
...
And then you're able to use it:
<Text style={[appStyles.customFontBold, styles.title1]}>
{ this.props.title }
</Text>
Scene takes a title param
<Scene
key="myPage"
component={MyPage}
navBar={NavBarCustom}
hideNavBar={false}
title="myPage"
/>