Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find length of variable in state in react native?

this.state = {
  phone: ''
};

I want to find the length of the phone variable in state on change so that if the length is 10 I can perform an action.

Also if possible can you let me know how can enable and disable native-base button programmatically in react native.

I'm a beginner in react native

like image 945
Sachin Avatar asked Oct 16 '25 02:10

Sachin


2 Answers

Get length of string

If the phone number is saved as a string then it is very easy. You can just do

let phoneNumberLength = this.state.phone.length

Then you can use it in an if statement

if (phoneNumberLength === 10) {
  // do something here
}

Native Base

The Button component in nativebase has a disabled prop that you can use. http://docs.nativebase.io/Components.html#button-disabled-headref

You could set a value in your state to control whether the button is disabled or not. Calling this.state({buttonDisabled: true}) will disable the button

this.state {
  buttonDisabled: false
}

<Button disabled={this.state.buttonDisabled} />
like image 162
Andrew Avatar answered Oct 17 '25 17:10

Andrew


Get length of string

If the phone number is saved as a string then it is very easy. You can just do

let phoneNumberLength = this.state.phone.length

Then you can use it in an if statement

if (phoneNumberLength === 10) {
  // do something here
}

Native Base

The Button component in nativebase has a disabled prop that you can use. http://docs.nativebase.io/Components.html#button-disabled-headref

You could set a value in your state to control whether the button is disabled or not. Calling this.state({buttonDisabled: true}) will disable the button

this.state {
  buttonDisabled: false
}

<Button disabled={this.state.buttonDisabled} />
like image 34
Andrew Avatar answered Oct 17 '25 16:10

Andrew