Guys i’m new to React Native and also not good at JavaScript.
In my app i have 2 TextInput and a Pressable button but the output is not making sense.
I’ve tried to change timing of execution of some lines but result is the same.
I’m using:
expo,
react native 0.68.2,
android api 30 (Android Studio)
output:
Android Bundling complete 32ms
First click
LOG description :
LOG identity :
Second click
LOG description :sadasdas
LOG identity :12345678910
Report.js
import { StyleSheet, View, TextInput, Pressable, Text } from "react-native";
import { useState, useEffect } from "react";
import axios from "axios";
export default function Report({ route, navigation }) {
const [enteredText, setEnteredText] = useState("");
const [enteredId, setEnteredId] = useState("");
const [category, setCategory] = useState("");
const id = route.params.category;
const [description, setDescription] = useState("");
const [identity, setIdentity] = useState("");
const { latitude, longitude } = [0, 0];
function getById(id) {
return axios.get("http:10.0.2.2:5000/api/Categories/getbyid", {
params: { id: id },
});
}
//console.log("id :" + getById(id));
useEffect(() => {
getById(id)
.then((result) => setCategory(result.data.data.categoryName))
.catch(function (error) {
console.error(
"There has been a problem with your fetch operation: " + error.message
);
// ADD THIS THROW error
throw error;
});
});
//console.log(category);
function descriptionInputHandler(enteredText) {
setEnteredText(enteredText);
//console.log("entered text :" + enteredText);
}
function identityInputHandler(enteredId) {
setEnteredId(enteredId);
//console.log("entered id :" + enteredId);
}
function buttonHandler() {
setDescription(enteredText);
setIdentity(enteredId);
setEnteredText("");
setEnteredId("");
console.log("description :" + description);
console.log("identity :" + identity);
}
return (
<View style={styles.container}>
<Text style={styles.title}>{category}</Text>
<View style={styles.inputContainer}>
<TextInput
style={styles.longTextInput}
placeholder="Describe your situation, don't worry we are here."
onChangeText={descriptionInputHandler}
value={enteredText}
/>
<TextInput
style={styles.textInput}
placeholder="What is your identity number."
onChangeText={identityInputHandler}
value={enteredId}
/>
<Pressable style={styles.button} onPress={buttonHandler}>
<Text style={styles.text}>Send report!</Text>
</Pressable>
</View>
</View>
);
}

