I'm consuming the QOD API and i can't seem to get the quote data from the Json. Here's what i have so far. I'm pretty sure i'm not mapping the data properly as the data is logging fine. (Please see the attached image), but i just can't seem to get it. I just need the author and imageUrl. Any help or direction is greatly appreciated.
export default class QuoteScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: true,
data: null,
error: null,
};
}
apiUrl = 'https://quotes.rest/qod';
getData = (ev) => {
this.setState({ loading: false, error: null });
let h = new Headers();
h.append('X-TheySaidSo-Api-Secret', '*******************My API key');
let req = new Request(this.apiUrl, {
headers: h,
method: 'GET',
});
fetch(req)
.then(response => response.json())
.then(this.showData)
.catch(this.ErrorMessage);
};
showData = (data) => {
this.setState({ loading: true, data });
console.log(data);
};
ErrorMessage = (err) => {
this.setState({ loading: true, error: err.message });
};
componentDidMount() { }
render() {
return (
<View styles={styles.container}>
{!this.state.loading && <Text>Loading</Text>}
<Text style={styles.txt}>Gimme Some Data</Text>
<Button title="GetData" onPress={this.getData} />
{this.state.error && (<Text style={styles.err}>{this.state.error}</Text>
)}
{this.state.data && this.state.data.length > 0 && (
this.state.data.map((contents) => (
<Text style={style.txt}>{contents.quotes}</Text>
))
)}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F4C724',
},
txt: {
fontSize: 24,
color: '#333'
},
err: {
color: 'red',
fontSize: 30,
fontWeight: 'bold'
}
});
You are putting whole array of object in Text which wouldn't be displaying anything
You need another map inside the current map:
!!contents.quotes?.length &&
contents.quotes.map(quote=>{
return <Text style={style.txt}>{quote.author}</Text>
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.