To handle Api call using onclick event in react js

533 Views Asked by At

**Hi everyone can anyone suggest me the easiest way to handle a api call using onclick in react js i tried other ways too but i always get the error "TypeError: Cannot read property 'viewdata' of undefined" can anyone suggest me other way [![enter image description here][1]][1] [1]: https://i.stack.imgur.com/aYOnu.png `enter co

//my api call
handleClick() {
        get("https://get-docs.p.rapidapi.com/api/Claim/GetDocumentList", {
            "content-type": "application/octet-stream",
            "x-rapidapi-host": "get-docs.p.rapidapi.com",
            "x-rapidapi-key": "dd9aad1168msh7ab81579e00d2f7p17fd9fjsnb9446ab25384",
            "useQueryString": true
        }, {"params": "claimId: dd61c35e-3edd-ea11-a813-000d3a795762"}
            , this.viewdata, (failure) => { console.log(failure) });
    }

//response if its success
viewdata = response => {        
        let viewdoc = [];
        for (let i = 0; i < response.data.length; i++) {
            let currentDoc = response.data[i];
            viewdoc.push({
                key: '' + i,
                document: currentDoc.caseTitle,
                filed_by: currentDoc.partyName,
                date: currentDoc.createdDate,
                status: currentDoc.documentStatus,
            });
        };
        this.setState({
            viewdoc: viewdoc,
        });
        console.log(viewdoc)
    }

//where am calling api in button
const columns = [
    {
        title: 'Document'       
    },
    {
        key: 'filed_by'
    },
    {
        dataIndex: 'date'
    },
    {
        title: 'Status'       
         render: (status) => (
            <span style={{ fontWeight: 600, color: "black" }}>{status}</span>
        ),
    },
    {
        title: 'Action',
        key: 'action',
        render: (text, record) => (
            <div size="small">
                <a href="#" onClick={this.handleClick.bind(this)}View File</a>
            </div>
        ),
    },
];

`**

0

There are 0 best solutions below