Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Says please attach method to this component

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

like image 424
Venky Avatar asked Jun 18 '18 15:06

Venky


1 Answers

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"
        />
like image 152
Bhekani Khumalo Avatar answered Nov 11 '22 04:11

Bhekani Khumalo