Reputation: 185
I using the this.props.navigation.navigate('Details',{activity: item})
to pass the item to a new page. And i want to make a fetch request by using this activity. I cannot get how to pass the item to the API request. Anyone can answer me?
register = async () =>{
var token = await AsyncStorage.getItem('id_token')
console.log(token)
fetch('http://192.168.0.1:8887/api/auth/activities/register',{
method: 'POST',
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' +token,
},
body: JSON.stringify({
})
})
}
render(){
const { activity } = this.props.route.params;
return (
<View style={styles.container}>
<ScrollView style={styles.content}>
<Text style={styles.data}>Title:</Text>
<Text style={styles.info}>{activity.name}</Text>
</ScrollView>
<Button title="register" onPress={this.register}></Button>
</View>
)
}
In this case, how can i pass the data to the fetch?
Upvotes: 0
Views: 697
Reputation: 4741
All other answers suggesting you should add it to your state are wrong. You can retrieve the value just like you're already doing in your render
function.
register = async () =>{
const { activity } = this.props.route.params;
const token = await AsyncStorage.getItem('id_token')
fetch('http://192.168.0.125:8887/api/auth/activities/register',{
method: 'POST',
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' +token,
},
body: JSON.stringify({ activity })
});
}
Upvotes: 2