I am trying to print out array's value, converting into string in the description of mapview marker.
when I put <Text>nameList</text> inside of return() under <View style={styles.container}> is okay, but description={nameList.toString()} in is just print out {object,object}, {object,object}, {object,object}
Please let me know how would I solve.
constructor(){
super()
this.state = {name: []}
}
componentDidMount(){
return fetch('https://transportapi.com/v3/uk/bus/stop/6090282/live.jsonapp_id=8425e834&app_key=a9de6fe50081ecf38d2e9c9d5f1a87e0&group=route&nextbuses=yes')
.then((response) => response.json())
.then((responseJson) => {
for(var x in responseJson.departures){
this.setState({
state : this.state["name"].push(responseJson.departures[x][0])
});
}
})
.catch((error) => {
console.error(error);
});
}
render() {
const info = this.state.name;
const nameList = info.map(name =>{
return(
<Text key={name.line}>{name.line}{name.aimed_departure_time}</Text>
)
})
return (
<View style={styles.container}>
<MapView
initialRegion={{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
>
<MapView.Marker
coordinate={{
latitude: 37.78825,
longitude: -122.4324,
}}
title={'Hello'}
description={nameList.toString()}
/>
</MapView>
</View>
}
nameLista Text Component and it’s inner text content can’t be extracted using.toString(). Use a string instead, see Vipin’s edited answer below for a solutionnameListmap and then usenameList.join(',')to create a full string from the array.