skip to Main Content

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?

2

Answers


  1. 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"):

    Login or Signup to reply.
  2. You can convert your string value to integer using parseInt().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search