【发布时间】:2018-11-27 15:02:58
【问题描述】:
我构建了一个组件,它使用 Axios 获取请求并检索电子邮件地址列表。
我不知道应该在 render() 中写什么,这样我才能在我的网站上看到电子邮件列表。
这是我的建议:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {Link} from "react-router";
import axios from 'axios';
export class GetEmailsComponent extends Component {
state = {
emails: []
}
componentDidMount(){
//this.setState({emailList : undefined});
axios.get('./api/EmailAddresses')
.then(response => {
this.setState({emails: response.data});
console.log(response.data);
}).catch(function (error) {
console.log(error);
});
}
render() {
return (
<div>
<button type = "button" onClick= {this.state.emails.map(email => <div>{email}</div>)}>GET ALL EMAILS</button>
</div>
);
}
}
当我检查控制台时,我会看到一组所需的电子邮件。 我正在寻找有关如何编辑我的代码的建议,以便它将所有这些邮件呈现到屏幕上(单击按钮后)。
先谢谢了。
【问题讨论】:
标签: javascript reactjs react-component