React Native is ignoring state change









up vote
0
down vote

favorite












Please explain why this isn't working. I am trying to replace one element with another on a button press in React, and, according to all documentation, what I am doing should work. However, React is entirely ignoring state changes and refusing to re-render an element. Here is my file:



import React, Component from 'react';
import View, Text, TextInput, TouchableOpacity from 'react-native';
import styles from '../styles';
import User from '../models/User';
export default class SignIn extends Component
constructor(props)
super(props);
this.state =
email: '',
password: '',
signIn: '',
errorMessage: null,
signUp: true


signIn()
var err = e => this.setState(errorMessage: e.message);
var next = u => this.props.navigation.push('Details');
User.login(this.state.email, this.state.password, err, next);

signUp()
var err = e => this.setState(errorMessage: e.message);
var next = _ => this.props.navigation.push('Details');
var user = new User(this.state.email, this.state.password);
user.save(err, next);

render()
return (
<View style=flex: 1, alignItems: 'center', justifyContent: 'center'>
<View style=flexDirection: 'row', width: 300, justifyContent: 'center'>
<TouchableOpacity onPress=_ => this.setState(signUp: true) style=[styles.navButton, styles.tabsButton]>
<Text style=[styles.buttonText, styles.tabs]>Sign Up</Text>
</TouchableOpacity>
<TouchableOpacity onPress=_ => this.setState(signUp: false) style=[styles.grey, styles.tabsButton]>
<Text style=[styles.greyText, styles.tabs]>Sign In</Text>
</TouchableOpacity>
</View>
this.state.signUp && <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
!this.state.signUp && <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
</View>
)




The Sign In button at the top should change the state and cause a Sign In form to appear and the Sign Up form to disappear, while the Sign Up button should do the opposite. If I console.log the state after the change, it has changed, but nothing else happens. What is going on? All documentation shows that you can do this, but it doesn't work for me. I am running the app on an Android phone over Expo.










share|improve this question





















  • I don't quite understand your code. Can you explain why regardless the value of this.state.signUp you are rendering the same component?
    – Isaac
    Nov 12 at 1:29










  • Sorry, I figured out the issue but I can't yet close the question
    – Isaac Krementsov
    Nov 13 at 13:46














up vote
0
down vote

favorite












Please explain why this isn't working. I am trying to replace one element with another on a button press in React, and, according to all documentation, what I am doing should work. However, React is entirely ignoring state changes and refusing to re-render an element. Here is my file:



import React, Component from 'react';
import View, Text, TextInput, TouchableOpacity from 'react-native';
import styles from '../styles';
import User from '../models/User';
export default class SignIn extends Component
constructor(props)
super(props);
this.state =
email: '',
password: '',
signIn: '',
errorMessage: null,
signUp: true


signIn()
var err = e => this.setState(errorMessage: e.message);
var next = u => this.props.navigation.push('Details');
User.login(this.state.email, this.state.password, err, next);

signUp()
var err = e => this.setState(errorMessage: e.message);
var next = _ => this.props.navigation.push('Details');
var user = new User(this.state.email, this.state.password);
user.save(err, next);

render()
return (
<View style=flex: 1, alignItems: 'center', justifyContent: 'center'>
<View style=flexDirection: 'row', width: 300, justifyContent: 'center'>
<TouchableOpacity onPress=_ => this.setState(signUp: true) style=[styles.navButton, styles.tabsButton]>
<Text style=[styles.buttonText, styles.tabs]>Sign Up</Text>
</TouchableOpacity>
<TouchableOpacity onPress=_ => this.setState(signUp: false) style=[styles.grey, styles.tabsButton]>
<Text style=[styles.greyText, styles.tabs]>Sign In</Text>
</TouchableOpacity>
</View>
this.state.signUp && <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
!this.state.signUp && <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
</View>
)




The Sign In button at the top should change the state and cause a Sign In form to appear and the Sign Up form to disappear, while the Sign Up button should do the opposite. If I console.log the state after the change, it has changed, but nothing else happens. What is going on? All documentation shows that you can do this, but it doesn't work for me. I am running the app on an Android phone over Expo.










share|improve this question





















  • I don't quite understand your code. Can you explain why regardless the value of this.state.signUp you are rendering the same component?
    – Isaac
    Nov 12 at 1:29










  • Sorry, I figured out the issue but I can't yet close the question
    – Isaac Krementsov
    Nov 13 at 13:46












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Please explain why this isn't working. I am trying to replace one element with another on a button press in React, and, according to all documentation, what I am doing should work. However, React is entirely ignoring state changes and refusing to re-render an element. Here is my file:



import React, Component from 'react';
import View, Text, TextInput, TouchableOpacity from 'react-native';
import styles from '../styles';
import User from '../models/User';
export default class SignIn extends Component
constructor(props)
super(props);
this.state =
email: '',
password: '',
signIn: '',
errorMessage: null,
signUp: true


signIn()
var err = e => this.setState(errorMessage: e.message);
var next = u => this.props.navigation.push('Details');
User.login(this.state.email, this.state.password, err, next);

signUp()
var err = e => this.setState(errorMessage: e.message);
var next = _ => this.props.navigation.push('Details');
var user = new User(this.state.email, this.state.password);
user.save(err, next);

render()
return (
<View style=flex: 1, alignItems: 'center', justifyContent: 'center'>
<View style=flexDirection: 'row', width: 300, justifyContent: 'center'>
<TouchableOpacity onPress=_ => this.setState(signUp: true) style=[styles.navButton, styles.tabsButton]>
<Text style=[styles.buttonText, styles.tabs]>Sign Up</Text>
</TouchableOpacity>
<TouchableOpacity onPress=_ => this.setState(signUp: false) style=[styles.grey, styles.tabsButton]>
<Text style=[styles.greyText, styles.tabs]>Sign In</Text>
</TouchableOpacity>
</View>
this.state.signUp && <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
!this.state.signUp && <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
</View>
)




The Sign In button at the top should change the state and cause a Sign In form to appear and the Sign Up form to disappear, while the Sign Up button should do the opposite. If I console.log the state after the change, it has changed, but nothing else happens. What is going on? All documentation shows that you can do this, but it doesn't work for me. I am running the app on an Android phone over Expo.










share|improve this question













Please explain why this isn't working. I am trying to replace one element with another on a button press in React, and, according to all documentation, what I am doing should work. However, React is entirely ignoring state changes and refusing to re-render an element. Here is my file:



import React, Component from 'react';
import View, Text, TextInput, TouchableOpacity from 'react-native';
import styles from '../styles';
import User from '../models/User';
export default class SignIn extends Component
constructor(props)
super(props);
this.state =
email: '',
password: '',
signIn: '',
errorMessage: null,
signUp: true


signIn()
var err = e => this.setState(errorMessage: e.message);
var next = u => this.props.navigation.push('Details');
User.login(this.state.email, this.state.password, err, next);

signUp()
var err = e => this.setState(errorMessage: e.message);
var next = _ => this.props.navigation.push('Details');
var user = new User(this.state.email, this.state.password);
user.save(err, next);

render()
return (
<View style=flex: 1, alignItems: 'center', justifyContent: 'center'>
<View style=flexDirection: 'row', width: 300, justifyContent: 'center'>
<TouchableOpacity onPress=_ => this.setState(signUp: true) style=[styles.navButton, styles.tabsButton]>
<Text style=[styles.buttonText, styles.tabs]>Sign Up</Text>
</TouchableOpacity>
<TouchableOpacity onPress=_ => this.setState(signUp: false) style=[styles.grey, styles.tabsButton]>
<Text style=[styles.greyText, styles.tabs]>Sign In</Text>
</TouchableOpacity>
</View>
this.state.signUp && <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
!this.state.signUp && <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
</View>
)




The Sign In button at the top should change the state and cause a Sign In form to appear and the Sign Up form to disappear, while the Sign Up button should do the opposite. If I console.log the state after the change, it has changed, but nothing else happens. What is going on? All documentation shows that you can do this, but it doesn't work for me. I am running the app on an Android phone over Expo.







reactjs react-native react-state-management react-state






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 15:16









Isaac Krementsov

14816




14816











  • I don't quite understand your code. Can you explain why regardless the value of this.state.signUp you are rendering the same component?
    – Isaac
    Nov 12 at 1:29










  • Sorry, I figured out the issue but I can't yet close the question
    – Isaac Krementsov
    Nov 13 at 13:46
















  • I don't quite understand your code. Can you explain why regardless the value of this.state.signUp you are rendering the same component?
    – Isaac
    Nov 12 at 1:29










  • Sorry, I figured out the issue but I can't yet close the question
    – Isaac Krementsov
    Nov 13 at 13:46















I don't quite understand your code. Can you explain why regardless the value of this.state.signUp you are rendering the same component?
– Isaac
Nov 12 at 1:29




I don't quite understand your code. Can you explain why regardless the value of this.state.signUp you are rendering the same component?
– Isaac
Nov 12 at 1:29












Sorry, I figured out the issue but I can't yet close the question
– Isaac Krementsov
Nov 13 at 13:46




Sorry, I figured out the issue but I can't yet close the question
– Isaac Krementsov
Nov 13 at 13:46












1 Answer
1






active

oldest

votes

















up vote
-1
down vote



accepted













this.state.signUp 
? <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
: <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>





If this wouldn't work, maybe styles?






share|improve this answer




















  • @IsaacKrementsov: How can this be answer? It doesnt make sense
    – Isaac
    Nov 13 at 13:57










  • Did you try and did it work?
    – Ziyo
    Nov 13 at 16:47










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250129%2freact-native-is-ignoring-state-change%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
-1
down vote



accepted













this.state.signUp 
? <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
: <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>





If this wouldn't work, maybe styles?






share|improve this answer




















  • @IsaacKrementsov: How can this be answer? It doesnt make sense
    – Isaac
    Nov 13 at 13:57










  • Did you try and did it work?
    – Ziyo
    Nov 13 at 16:47














up vote
-1
down vote



accepted













this.state.signUp 
? <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
: <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>





If this wouldn't work, maybe styles?






share|improve this answer




















  • @IsaacKrementsov: How can this be answer? It doesnt make sense
    – Isaac
    Nov 13 at 13:57










  • Did you try and did it work?
    – Ziyo
    Nov 13 at 16:47












up vote
-1
down vote



accepted







up vote
-1
down vote



accepted









this.state.signUp 
? <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
: <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>





If this wouldn't work, maybe styles?






share|improve this answer















this.state.signUp 
? <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
: <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>





If this wouldn't work, maybe styles?






this.state.signUp 
? <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
: <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>





this.state.signUp 
? <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=styles.navButton>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>
: <View>
<Text style=color: 'red'>this.state.errorMessage</Text>
<TextInput
placeholder='Email'
style=styles.input
value=this.state.email
onChangeText=(email) => this.setState(email)
/>
<TextInput
placeholder='Password'
style=styles.input
secureTextEntry=true
onChangeText=(password) => this.setState(password)
/>
<TouchableOpacity style=[styles.navButton]>
<Text style=styles.buttonText>Sign Up</Text>
</TouchableOpacity>
</View>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 1:17









Ziyo

21819




21819











  • @IsaacKrementsov: How can this be answer? It doesnt make sense
    – Isaac
    Nov 13 at 13:57










  • Did you try and did it work?
    – Ziyo
    Nov 13 at 16:47
















  • @IsaacKrementsov: How can this be answer? It doesnt make sense
    – Isaac
    Nov 13 at 13:57










  • Did you try and did it work?
    – Ziyo
    Nov 13 at 16:47















@IsaacKrementsov: How can this be answer? It doesnt make sense
– Isaac
Nov 13 at 13:57




@IsaacKrementsov: How can this be answer? It doesnt make sense
– Isaac
Nov 13 at 13:57












Did you try and did it work?
– Ziyo
Nov 13 at 16:47




Did you try and did it work?
– Ziyo
Nov 13 at 16:47

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250129%2freact-native-is-ignoring-state-change%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto