I starting learning React a couple days ago and Axios today. I've spent the last 4+ hours watching/reading tutorials and I just can't figure this out.
I'm trying to create a simple stats website for League of Legends using Riot's API. Below you can see my constructor, componentDidMount, and render functions. I feel like I'm doing 1 of 3 wrong or most likely all 3. I'm calling this Get, which returns the JSON below. I want to access the "name" and "accountId".
{
"profileIconId": 3270,
"name": "Doublelift",
"puuid": "SrvIz_3Xa05InF_hTjwq1v8iB6lqNXz0SEc_5vhOFYlScrZOg8pSM9Si_UdPGAD9UYGhaRWHBeBGrw",
"summonerLevel": 155,
"accountId": "iNc_SUPKq-ckcANeC36Yn18Y0XSofK3ShBQg_h5wivC0Bg",
"id": "DjnxZhsTjgNhv3sMZMMJjlCUqAskiMfP6bP7GIcWovbwR1k",
"revisionDate": 1580499454000
}
I should note that I made my API key default. It's stored in my index.js file. Is this secure?
axios.defaults.headers.common['Authentication'] = 'API-Key-randomlettersandnumbers';
Here's my code. In render() when I type summonerDetail.[field]
it recognizes the fields that are there shown in the JSON response above. Maybe my render is wrong causing it not to display? And yes I know "accountID" isn't in my render. I figured I'd start small with just "name". I will eventually need to use "accountID" for a different Get.
import React from 'react';
import axios from 'axios';
export default class GetBySummonerName extends React.Component {
constructor(props){
super(props);
this.state = {
summonerDetails: []
};
}
// https request to perform Get
componentDidMount() {
axios.get('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/doublelift')
.then(res => {
console.log(res);
this.setState({ summonerDetails: res.data});
})
}
render() {
return (
<div>
{ this.state.summonerDetails.map(summonerDetail => <h1>{summonerDetail.name}</h1>)}
</div>
)
}
}
To display the "name" on the website I import the above class into App.js. Only problem is it's not working. I have the console.log(res); in my ComponentDidMount(), but I don't know how to view the console in Atom. I don't need any headers in my componentDidMount() because the "summonerName" is in the Get URL. The rest of the headers are auto-generated on Riot's side. Please help :)