I am new to react and started looking into pulling json data in. To test I am using: https://randomuser.me/api/?results=5
What I am trying to achieve is when I click on a username from json, it will console log the username. How do I pass the value from json to console log?
const API = 'https://randomuser.me/api/?results=5';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
logins: [],
};
}
componentDidMount() {
fetch(API)
.then(response => response.json())
.then(data => {
let logins = data.results.map((uname) => {
return (
<div>
<button>{uname.login.username}</button>
</div>
)
})
this.setState({logins: logins});
})
}
render() {
return (
<div>
{this.state.logins}
</div>
);
}
}
Is it a good idea to return html code in componentDidMount or should I leave it to render?
componentDidMount().