I am trying to make an API call through a separate file, and return results along with status like, displaying loading text until data is retrieved and being displayed.
In a single file it worked fine but now I moved to separate files like this, it's not working...........
UserDataTable
import React, { Component } from 'react';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';
import { withStyles } from '@material-ui/core/styles';
import { getUserData } from './APIcalls/UserSearch.js';
// Styles
const styles = theme => ({
Table: {
margin: '10px'
},
});
class UserDataTable extends Component {
componentDidMount() {
var config = { "Access-Control-Allow-Origin": "*" }
getUserData(config, () => {
// Success
console.log();
},(err) => {
// Error
alert(err);
});
}
render() {
const { classes } = this.props;
return (
<div>
<Paper className={classes.Table}>
<Table>
<TableHead className={classes.TableHeader}>
<TableRow>
<TableCell>Firm</TableCell>
<TableCell>Office</TableCell>
<TableCell>Sales Code</TableCell>
<TableCell>Account</TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell>1</TableCell>
<TableCell>2</TableCell>
<TableCell>3</TableCell>
<TableCell>4</TableCell>
</TableRow>
<TableRow>
<TableCell>1</TableCell>
<TableCell>2</TableCell>
<TableCell>3</TableCell>
<TableCell>4</TableCell>
</TableRow>
</TableBody>
</Table>
</Paper>
</div>
)
}
}
export default withStyles(styles)(UserDataTable);
getUserData API
import axios from 'axios';
export function getUserData(config, errorcallback){
axios.get('__LINK__', config)
.then(response => {
this.setState({
isLoaded: true,
items: response,
})
})
.catch(err => {
// catch error
if(errorcallback != null){
errorcallback(err);
}
})
}