Need help displaying a single JSON record from an API request on the screen.
const [user, setUser] = useState();
const getUserData = async () => {
// {headers: {Authorization: "Basic " + base64.encode(username + ":" + password),}}
try {
const response = await fetch('http://*******/warrant/' + route.params.id, {headers: {Authorization: "Basic " + base64.encode(username + ":" + password),}});
json = await response.json();
setUser(json.warrant);
} catch (error) {
console.error(error);
}
};
useState(() => {
getUserData();
}, []);
return (
<View >
<View >
<Text style={styles.input2}>name:</Text>
<Text style={styles.input} value={user.name}>{user.name}</Text>
</View>
Error: undefined is not an object (evaluating 'user.name')
Sample JSON data:
{"warrant": {"dateend": "1", "datestart": "1", "description": "1", "fio": "1", "id": 2, "image": "", "name": " 3", "number": "4", "numberitem": "4", "state": 2}}