Home > Net >  How can i get Number in onChangeText in React Native
How can i get Number in onChangeText in React Native

Time:09-29

I have created a userInput in React Native

const [first_name, setFirstName] = useState("");

  (<View style={styles.InputContainer}>
  <Text>Fisrt Name</Text>
  <TextInput
    value={first_name}
    onChangeText={(text) => setFirstName(text)}
    placeholder="John"
    style={styles.textInput}
  />
</View>;)


     

and first_name is a string
and I have another field for which I want a number

const [fabricLength, setFabricLength] = useState(0);

 (<View style={styles.InputContainer}>
                  <Text>S Fabric Length</Text>
                  <TextInput
                     value={fabricLength}
                     onValueChange={(text) => setFabricLength(text)}
                     placeholder="Enter Fabric Length"
                     keyboardType="number-pad"
                     style={styles.textInput}
              />
            </View>;)
    

whatever number I type I am still getting string How can I get number

CodePudding user response:

Text input is always returning string, if you want user to enter only numbers you can set keyboardType to numeric, but if you want to use them as number later in your app, just use Number("yourStringNumber"):

CodePudding user response:

You can convert your string value to integer using parseInt().

  • Related