How can I compare two variable in onPress?
I am trying to create a changing pin screen and i was failed in comparing two variable that getting from the user (new pin and comfirm pin). The error show me that "this.state.newpin" is an undefined object.
class SettingScreen extends Component {
state = {
oldpin: '000000',
newpin: '',
secpin: ''
}
onPressButton(){
if( this.state.newpin == this.state.secpin){
ToastAndroid.show("Password Changed", ToastAndroid.SHORT);
this.setState({ oldpin : this.state.newpin})
}
else {
ToastAndroid.show("Password Unmatched", ToastAndroid.SHORT);
}
}
handleNewPin = (text) => {
this.setState({ newpin: text })
}
handleSecPin = (text) => {
this.setState({ secpin: text })
}
...
<TextInput onChangeText = {this.handleNewPin} />
<TextInput onChangeText = {this.handleSecPin} />
<TouchableOpacity onPress={this.onPressButton}>
<Text> Change Password </Text>
</TouchableOpacity>
I can get the output for "this.state.newpin" and "this.state.secpin" from user.
I just failed in the comparing statement ( OnPressButton()).
I am new in React-Native.
Sorry for any inconvenience.
react-native comparison
add a comment |
I am trying to create a changing pin screen and i was failed in comparing two variable that getting from the user (new pin and comfirm pin). The error show me that "this.state.newpin" is an undefined object.
class SettingScreen extends Component {
state = {
oldpin: '000000',
newpin: '',
secpin: ''
}
onPressButton(){
if( this.state.newpin == this.state.secpin){
ToastAndroid.show("Password Changed", ToastAndroid.SHORT);
this.setState({ oldpin : this.state.newpin})
}
else {
ToastAndroid.show("Password Unmatched", ToastAndroid.SHORT);
}
}
handleNewPin = (text) => {
this.setState({ newpin: text })
}
handleSecPin = (text) => {
this.setState({ secpin: text })
}
...
<TextInput onChangeText = {this.handleNewPin} />
<TextInput onChangeText = {this.handleSecPin} />
<TouchableOpacity onPress={this.onPressButton}>
<Text> Change Password </Text>
</TouchableOpacity>
I can get the output for "this.state.newpin" and "this.state.secpin" from user.
I just failed in the comparing statement ( OnPressButton()).
I am new in React-Native.
Sorry for any inconvenience.
react-native comparison
add a comment |
I am trying to create a changing pin screen and i was failed in comparing two variable that getting from the user (new pin and comfirm pin). The error show me that "this.state.newpin" is an undefined object.
class SettingScreen extends Component {
state = {
oldpin: '000000',
newpin: '',
secpin: ''
}
onPressButton(){
if( this.state.newpin == this.state.secpin){
ToastAndroid.show("Password Changed", ToastAndroid.SHORT);
this.setState({ oldpin : this.state.newpin})
}
else {
ToastAndroid.show("Password Unmatched", ToastAndroid.SHORT);
}
}
handleNewPin = (text) => {
this.setState({ newpin: text })
}
handleSecPin = (text) => {
this.setState({ secpin: text })
}
...
<TextInput onChangeText = {this.handleNewPin} />
<TextInput onChangeText = {this.handleSecPin} />
<TouchableOpacity onPress={this.onPressButton}>
<Text> Change Password </Text>
</TouchableOpacity>
I can get the output for "this.state.newpin" and "this.state.secpin" from user.
I just failed in the comparing statement ( OnPressButton()).
I am new in React-Native.
Sorry for any inconvenience.
react-native comparison
I am trying to create a changing pin screen and i was failed in comparing two variable that getting from the user (new pin and comfirm pin). The error show me that "this.state.newpin" is an undefined object.
class SettingScreen extends Component {
state = {
oldpin: '000000',
newpin: '',
secpin: ''
}
onPressButton(){
if( this.state.newpin == this.state.secpin){
ToastAndroid.show("Password Changed", ToastAndroid.SHORT);
this.setState({ oldpin : this.state.newpin})
}
else {
ToastAndroid.show("Password Unmatched", ToastAndroid.SHORT);
}
}
handleNewPin = (text) => {
this.setState({ newpin: text })
}
handleSecPin = (text) => {
this.setState({ secpin: text })
}
...
<TextInput onChangeText = {this.handleNewPin} />
<TextInput onChangeText = {this.handleSecPin} />
<TouchableOpacity onPress={this.onPressButton}>
<Text> Change Password </Text>
</TouchableOpacity>
I can get the output for "this.state.newpin" and "this.state.secpin" from user.
I just failed in the comparing statement ( OnPressButton()).
I am new in React-Native.
Sorry for any inconvenience.
react-native comparison
react-native comparison
asked Jan 19 at 12:04
DMC EXCDMC EXC
225
225
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
you just need to bind your onPressButton()
func. in the constructor with this. and move your state to constructor like this;
class SettingScreen extends Component {
constructor(props) {
super(props);
this.state = {
oldpin: '000000',
newpin: '',
secpin: ''
};
this.onPressButton = this.onPressButton.bind(this);
}
}
I changed the code but the error still occurs at "onPressButton()"
– DMC EXC
Jan 19 at 12:22
you need to bind all handle functions. We must bind our functions which contain setState for accesing this.setState
– sdkcy
Jan 19 at 12:24
1
Since they are arrow functions they don't need to be bound. So the problem is just the onPress. You can bind like @sdkcy did in this answer, or you can make it an arrow functon like the others
– Milore
Jan 19 at 12:44
2
I've just created an example snack. Take a look
– Milore
Jan 19 at 12:55
1
Thank you so much. It's working now. Thank for helping.
– DMC EXC
Jan 19 at 13:07
|
show 4 more comments
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266903%2fhow-can-i-compare-two-variable-in-onpress%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
you just need to bind your onPressButton()
func. in the constructor with this. and move your state to constructor like this;
class SettingScreen extends Component {
constructor(props) {
super(props);
this.state = {
oldpin: '000000',
newpin: '',
secpin: ''
};
this.onPressButton = this.onPressButton.bind(this);
}
}
I changed the code but the error still occurs at "onPressButton()"
– DMC EXC
Jan 19 at 12:22
you need to bind all handle functions. We must bind our functions which contain setState for accesing this.setState
– sdkcy
Jan 19 at 12:24
1
Since they are arrow functions they don't need to be bound. So the problem is just the onPress. You can bind like @sdkcy did in this answer, or you can make it an arrow functon like the others
– Milore
Jan 19 at 12:44
2
I've just created an example snack. Take a look
– Milore
Jan 19 at 12:55
1
Thank you so much. It's working now. Thank for helping.
– DMC EXC
Jan 19 at 13:07
|
show 4 more comments
you just need to bind your onPressButton()
func. in the constructor with this. and move your state to constructor like this;
class SettingScreen extends Component {
constructor(props) {
super(props);
this.state = {
oldpin: '000000',
newpin: '',
secpin: ''
};
this.onPressButton = this.onPressButton.bind(this);
}
}
I changed the code but the error still occurs at "onPressButton()"
– DMC EXC
Jan 19 at 12:22
you need to bind all handle functions. We must bind our functions which contain setState for accesing this.setState
– sdkcy
Jan 19 at 12:24
1
Since they are arrow functions they don't need to be bound. So the problem is just the onPress. You can bind like @sdkcy did in this answer, or you can make it an arrow functon like the others
– Milore
Jan 19 at 12:44
2
I've just created an example snack. Take a look
– Milore
Jan 19 at 12:55
1
Thank you so much. It's working now. Thank for helping.
– DMC EXC
Jan 19 at 13:07
|
show 4 more comments
you just need to bind your onPressButton()
func. in the constructor with this. and move your state to constructor like this;
class SettingScreen extends Component {
constructor(props) {
super(props);
this.state = {
oldpin: '000000',
newpin: '',
secpin: ''
};
this.onPressButton = this.onPressButton.bind(this);
}
}
you just need to bind your onPressButton()
func. in the constructor with this. and move your state to constructor like this;
class SettingScreen extends Component {
constructor(props) {
super(props);
this.state = {
oldpin: '000000',
newpin: '',
secpin: ''
};
this.onPressButton = this.onPressButton.bind(this);
}
}
answered Jan 19 at 12:10
sdkcysdkcy
60616
60616
I changed the code but the error still occurs at "onPressButton()"
– DMC EXC
Jan 19 at 12:22
you need to bind all handle functions. We must bind our functions which contain setState for accesing this.setState
– sdkcy
Jan 19 at 12:24
1
Since they are arrow functions they don't need to be bound. So the problem is just the onPress. You can bind like @sdkcy did in this answer, or you can make it an arrow functon like the others
– Milore
Jan 19 at 12:44
2
I've just created an example snack. Take a look
– Milore
Jan 19 at 12:55
1
Thank you so much. It's working now. Thank for helping.
– DMC EXC
Jan 19 at 13:07
|
show 4 more comments
I changed the code but the error still occurs at "onPressButton()"
– DMC EXC
Jan 19 at 12:22
you need to bind all handle functions. We must bind our functions which contain setState for accesing this.setState
– sdkcy
Jan 19 at 12:24
1
Since they are arrow functions they don't need to be bound. So the problem is just the onPress. You can bind like @sdkcy did in this answer, or you can make it an arrow functon like the others
– Milore
Jan 19 at 12:44
2
I've just created an example snack. Take a look
– Milore
Jan 19 at 12:55
1
Thank you so much. It's working now. Thank for helping.
– DMC EXC
Jan 19 at 13:07
I changed the code but the error still occurs at "onPressButton()"
– DMC EXC
Jan 19 at 12:22
I changed the code but the error still occurs at "onPressButton()"
– DMC EXC
Jan 19 at 12:22
you need to bind all handle functions. We must bind our functions which contain setState for accesing this.setState
– sdkcy
Jan 19 at 12:24
you need to bind all handle functions. We must bind our functions which contain setState for accesing this.setState
– sdkcy
Jan 19 at 12:24
1
1
Since they are arrow functions they don't need to be bound. So the problem is just the onPress. You can bind like @sdkcy did in this answer, or you can make it an arrow functon like the others
– Milore
Jan 19 at 12:44
Since they are arrow functions they don't need to be bound. So the problem is just the onPress. You can bind like @sdkcy did in this answer, or you can make it an arrow functon like the others
– Milore
Jan 19 at 12:44
2
2
I've just created an example snack. Take a look
– Milore
Jan 19 at 12:55
I've just created an example snack. Take a look
– Milore
Jan 19 at 12:55
1
1
Thank you so much. It's working now. Thank for helping.
– DMC EXC
Jan 19 at 13:07
Thank you so much. It's working now. Thank for helping.
– DMC EXC
Jan 19 at 13:07
|
show 4 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266903%2fhow-can-i-compare-two-variable-in-onpress%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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