I'm new to react native and i'm trying to send some data to my API,basically a POST request,added a button and trying to call the fetch request using that,but i'm not sure what am i doing wrong in this ?? It says "Please attach method to this component" in the log,let me know if i'm doing anything wrong in this ??
Here's my button
<Button
style={{height: 60,width:150}}
onClick={ () => this.submitNewCustomer()}
title="SUBMIT"
backgroundColor='#C0C0C0'
color="black"
/>
this is my method
submitNewCustomer(){
fetch('http://endpoint.net/api/customerdetail', {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json', // <-- Specifying the Content-Type
}),
body: JSON.stringify({
CustomerId: '1ef87a90-a941-4ebb-b101-66f74ac07778',
CustomerName: this.state.customername,
UserId:'user2',
VehicleCompanyName:this.state.vehiclecompanyname,
VehicleModelType:this.state.vehiclemodeltype,
VehicleNumber:this.state.vehiclenumber,
CustomerImage:'',
Location:'',
CustomerImageType:'png'
}), // <-- Post parameters
})
.then((response) => response.text())
.then((responseText) => {
alert(responseText);
})
.catch((error) => {
console.error(error);
});
}
Any Inputs would be really helpfull
You need to use onPress
instead of `onClick, so your button should be
<Button
style={{height: 60,width:150}}
onPress={ () => this.submitNewCustomer()}
title="SUBMIT"
backgroundColor='#C0C0C0'
color="black"
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With