I am building a project in react which uses images as buttons and by pressing the button an API is called, which renders information on another screen. Each image button contains a paramater (route) which is put into the API and so renders different information for each button pressed.
I coded the paramater which is called route and also the buttons, however when the button is pressed, I get a syntax error: [SyntaxError: JSON Parse error: Unrecognized token '<']. And on the console it prints empty string.
Any help would be appreciated.
Below is the code:
Here is the homepage which contains the buttons as images:
import { Dimensions, StyleSheet, Text, View, ImageBackground, Image, TouchableOpacity} from 'react-native';
import React, { Component, useState} from 'react';
export default function HomePage({ navigation }) {
const localImage = require('../../assets/starnight.jpg')
return (
<ImageBackground source={localImage }style={styles.container}>
<View style={{flex:1,justifyContent:"center",alignItems:"center"}}>
<View
style = {{
borderRadius: Math.round(Dimensions.get('window').width + Dimensions.get('window').height) / 2,
width: Dimensions.get('window').width * 0.8,
height: Dimensions.get('window').width * 0.8,
borderWidth:5,
borderColor:"transparent",
justifyContent: 'center',
alignItems: 'center'
}}
underlayColor = '#ccc'
>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "aries" })}>
<Image source={require('../../assets/aries.png')} style={{height:60,width:60,position:"absolute",bottom:Dimensions.get('window').width * 0.1, left:-70}} />
</ TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "taurus" })}>
<Image source={require('../../assets/taurus.png')} style={{height:60,width:60,position:"absolute",bottom:Dimensions.get('window').width * 0.1, right: -70}} />
</ TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "gemini" })}>
<Image source={require('../../assets/gemini.png')} style={{height:60,width:60,position:"absolute",bottom:Dimensions.get('window').width * 0.001,right: -135}} />
</TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "cancer" })}>
<Image source={require('../../assets/cancer.png')} style={{height:60,width:60,position:"absolute",top:Dimensions.get('window').width * 0.01,right:-170}} />
</TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "leo" })}>
<Image source={require('../../assets/leo.png')} style={{height:60,width:60,position:"absolute",top:Dimensions.get('window').width * 0.2,right:-170}} />
</TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "virgo" })}>
<Image source={require('../../assets/virgo.png')} style={{height:60,width:60,position:"absolute",top:Dimensions.get('window').width * 0.38,right:-135}} />
</TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "libra" })}>
<Image source={require('../../assets/libra.png')} style={{height:60,width:60,position:"absolute",top:Dimensions.get('window').width * 0.4,right: -60}} />
</TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "scorpio" })}>
<Image source={require('../../assets/scorpio.png')} style={{height:60,width:60,position:"absolute",top:Dimensions.get('window').width * 0.4,left: -60}} />
</TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "sagittarius" })}>
<Image source={require('../../assets/sagittarius.png')} style={{height:60,width:60,position:"absolute",top:Dimensions.get('window').width * 0.38,left:-135}} />
</TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "capricorn" })}>
<Image source={require('../../assets/capricorn.png')} style={{height:60,width:60,position:"absolute",top:Dimensions.get('window').width * 0.2,left:-170}} />
</TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "aquarius" })}>
<Image source={require('../../assets/aquarius.png')} style={{height:60,width:60,position:"absolute",top:Dimensions.get('window').width * 0.02,left:-170}} />
</TouchableOpacity>
<TouchableOpacity onPress={() => navigation.navigate('Horoscope', { sign: "pisces" })}>
<Image source={require('../../assets/pisces.png')} style={{height:60,width:60,position:"absolute",bottom:Dimensions.get('window').width * 0.001,left: -135}} />
</TouchableOpacity>
<Text style={styles.title}>Astrology</Text>
<Text style={styles.subheader}>Click on your star sign to get your daily horoscope</Text>
</View>
</View>
</ImageBackground>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
title: {
color: 'white',
fontWeight: 'bold',
fontSize: 40,
},
header: {
color: "red",
fontWeight: "bold",
fontSize: 20,
},
subheader: {
color: "white",
fontWeight: "bold",
fontSize: 16,
textAlign: 'center',
paddingLeft: 50,
paddingRight: 50
},
text: {
color: "black",
fontSize: 14,
}
});
Here is the code for the horoscope page which is where the API is called and the info from the API is returned.
import React, { useState, useEffect } from "react";
import { Text, View, StyleSheet, ImageBackground } from 'react-native';
import axios from "axios";
export default function Zodiac({ route }) {
const [isLoading, setIsLoading] = useState(true);
const [data, setData] = useState([]);
const { sign } = route.params;
useEffect(() => {
const url = `https://aztro.sameerkumar.website/?sign=${route}&day=today`;
fetch(url)
.then((response) => response.json())
.then((json) => setData(json['results']))
.catch((error) => console.log(error));
}, []);
useEffect(() => {
if (data.length !== 0) {
setIsLoading(false);
}
console.log(data);
}, [data]);
return (
<ImageBackground styles={styles.container} source={require('../../assets/starnight.jpg')}>
{isLoading ? (
<Text style={styles.title}>Loading... {sign}</Text>
) : (
<View >
<Text style={styles.title}>{sign}</Text>
<Text style={styles.text1}> {data.date_range} </Text>
<Text style={styles.text1}>Today: {data.current_date}</Text>
<Text style={styles.text1}> Description: {data.description} </Text>
<View style={styles.here1}>
<ImageBackground
style={styles.Image1}
source={require('../../assets/glitter-star.png')}>
<View style={styles.textView}>
<Text style={styles.text}>Compatibility: {data.compatibility} </Text>
</View>
</ImageBackground>
<ImageBackground
style={styles.Image1}
source={require('../../assets/glitter-star.png')}>
<View style={styles.textView}>
<Text style={styles.text}>Lucky Number: {data.lucky_number}</Text>
</View>
</ImageBackground>
</View>
<View style={styles.here2}>
<ImageBackground
style={styles.Image1}
source={require('../../assets/glitter-star.png')}>
<View style={styles.textView}>
<Text style={styles.text}>Lucky Time: {data.lucky_time} </Text>
</View>
</ImageBackground>
<ImageBackground
style={styles.Image1}
source={require('../../assets/glitter-star.png')}>
<View style={styles.textView}>
<Text style={styles.text}> Color: {data.color} </Text>
</View>
</ImageBackground>
</View>
<View style={styles.here3}>
<ImageBackground
style={styles.Image1}
source={require('../../assets/glitter-star.png')}>
<View style={styles.textView}>
<Text style={styles.text}> Mood: {data.mood} </Text>
</View>
</ImageBackground>
</View>
</View>
)}
</ImageBackground>
)};
const styles = StyleSheet.create({
container: {
flex: 1,
width: null,
height: null,
},
title: {
textAlign: 'center',
fontWeight: 'bold',
fontSize: 40,
color: 'white'
},
text: {
color: "white",
fontSize: 10,
textAlign: 'center',
paddingTop: 70,
padding: 55
},
text1: {
color: "white",
fontWeight: "bold",
fontSize: 10,
textAlign: 'center',
margin: 10
},
here1: {
flexDirection: 'row',
justifyContent: "center"
},
here2: {
flexDirection: 'row',
justifyContent: "center",
margin: -20
},
here3: {
flexDirection: 'row',
justifyContent: "center",
margin: -24
},
Image1: {
width: 185,
height: 185,
}
});