Repository navigation
6b api cors
Ben Foley edited this page Sep 1, 2019
·
1 revision
TODO: be more verbose
Add proxy to package.json. Match the URL to the part of the API service you want to use.
This is a snippet
{
"name": "react-axios",
"version": "0.1.0",
"private": true,
"proxy": "https://api.trove.nla.gov.au/v2",
"dependencies": {
...
}
Create the URL that axios refers to in App.js
and update the render to suit the data format
import React from 'react'
import axios from 'axios';
class App extends React.Component {
state = {
items: [],
key: '8odl5mlnqm4pr15a',
zone: 'book',
query: 'sharks'
}
componentDidMount() {
const url = '/result?key=' + this.state.key + '&zone=' + this.state.zone + '&q=' + this.state.query
axios.get(url)
.then(response => {
// APIs give response data back in different formats,
// check what the format is by looking at the console log
console.log(response)
const items = response.data.response.zone[0].records.work
this.setState({ items: items })
})
}
render() {
return (
<ul>
{
// map will loop over item in items
this.state.items.map(item => {
return (
<li className="item" key={item.id}>
{item.title}
</li>
)
})
}
</ul>
)
}
}
export default App;