Getting Started with React Native: Taking input from the user

Search for a command to run...

No comments yet. Be the first to comment.
So, I am going to continue my series of Getting Started with React native in this section we would learn how to use images in the bare workflow of react-native. well, I would not be using Expo here since it has its own expo camera. just fire up your ...
Hi, Hope you are doing fine... Gaurav This side, I hope everything is fine, on your side, and if it is not fine, don't worry it would be : ) This article is regarding AnonMsg which you to give and take Anonymous feedback from anyone. Feedback has al...

Welcome to JavaScript baazi a blog series where we go through common interview questions in JavaScript and understand everything around them. Firstly What is Promise.all() ? So... Promise.all() take an array of promises as input and then return a pro...

In this blog, we are going to build a QR code scanner and generator in Next.js. QR codes are currently powering everything in the world. from payments to id cards, everything has a QR code on it. Here in this small demo we will be building a small de...

Welcome to JavaScript baazi a blog series where we go through common interview questions in JavaScript and understand everything around them. Hmm... So what is the flattening of an array? basically in flattening we reduce the dimension of an array.ba...

Hello 👋 Gaurav This side, I hope everything is fine, on your side, and if it is not fine, don't worry it would be : ) This article is regarding MeowForms which allows you to build custom back endless for free. I have made my small side projects in ...

Here in this blog, I would be discussing how to take input from the user and how I build a simple currency converter in react native. So, let's open our snack powered by the expo, now in this project, I would be using Alert, SafeAearView, and ScrollView. The alert helps us to notify the user, SafeAreaView places our content in such a way that it is safe from the notch and cuts of the fancy screens. and why ScrollAreaView? well, we would be taking input from the user here, SafeAreaView has a special prop keyboardDismissMode which when set to true m, helps to exit the keyboard when the user taps anywhere on the screen.
Also, we would be taking input from the user and using the state to update that input in one go. So we would be using the useState hook here as well.
import React ,{useState} from 'react';
import { StyleSheet, Text, View ,SafeAreaView ,ScrollView ,TextInput, TouchableOpacity ,Alert} from 'react-native';
now since it is a currency converter app we need to get values from somewhere, so I would be making an object and later mapping through each value.
{Object.keys(currencyPerRuppe).map((currency)=>(
<TouchableOpacity key={currency} style={styles.currencyButton} onPress={()=>buttonPress(currency)}
><Text style={styles.currencyText}>{currency}</Text></TouchableOpacity>
))}
now, I would be working on the input part, I have used the use of the state hook and set the initial value to 0. now I would be taking value from the TextInput and finally updating the value to using setState. Also, text input provides us the option to change the keyboard type, and do remember to set the keyboard to dismiss mode as true in the scroll view.
<TextInput style={styles.inputValue} keyboardType="numeric"
placeholderTextColor="#c1c1c1" placeholder="Value"
value={inputValue} onChangeText={(inputValue)=> setInputValue(inputValue)}
></TextInput>
now finally we just need to calculate the converted value of the currency on the button press, so I would be making a function to do that also we need to handle an expectation what if the user pressed the button without entering any value? well, we can check that and give an alert to the user.
const buttonPress = (currency) => {
if(!inputValue){
Alert.alert(
"Please enter a correct value",
"Enter a numeric value",
[
{ text: "OK", onPress: () => console.log("OK Pressed") }
],
);
}
let result = parseFloat(inputValue) * currencyPerRuppe[currency] ;
setResultValue(result.toFixed(2));
}
here the snack of this app, if you want to check the whole app or code.
wow! so you read the whole article man!! who does that.. get a life man.