Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Is there a way to handle number inputs instead of string with React Native?

$
0
0

I have a react context state for my multi form input values:

  const [formValues, setFormValues] = useState({    sex: "male",    unitSystem: "metric",    heightInCm: "173",    weightInKg: "60",    dateOfBirth: null,  });

and I am struggling to undestand how I should handle my number inputs, like height and weight. At the moment I have to store them in a string format, because TextInput only accepts a string. I find myself having to keep converting string to number for validations.

For example this is my input handler:

  const toCm = (foot: string, inches: string) => {    const footInCm = convert(parseInt(foot)).from("ft").to("cm");    const inchesToCm = convert(parseInt(inches)).from("in").to("cm");    const actualCm = footInCm + inchesToCm;    return actualCm;  };  const handleImperialSubmit = () => {    toCm(foot, inches);    setFormValues((prev) => ({      ...prev,      heightInCm: toCm(foot, inches).toString(),    }));    setModalVisible(!modalVisible);  };

is there a way to work with actual numbers in react native, because this is very confusing.


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>