Javascript 希望从componentDidMount传递数据,但它';它总是未定义的
从componentdidmount中的服务器获取数据并传递到handlesubmit函数,但它始终返回未定义的:Javascript 希望从componentDidMount传递数据,但它';它总是未定义的,javascript,django,reactjs,django-rest-framework,Javascript,Django,Reactjs,Django Rest Framework,从componentdidmount中的服务器获取数据并传递到handlesubmit函数,但它始终返回未定义的: constructor() { super(); this.state = { data: [] }; } componentDidMount() { fetch(BASE_URL_API + "/payments/subscriptions/") .then(res => res.json()) .then(
constructor() {
super();
this.state = { data: [] };
}
componentDidMount() {
fetch(BASE_URL_API + "/payments/subscriptions/")
.then(res => res.json())
.then(json => this.setState({ data: json }));
}
handleSubmit = async event => {
event.preventDefault();
const data = this.props;
console.log("Token is: ", data);
};
如果我正确理解你的问题,它应该是
constdata={this.state}代码>因为您正在状态中存储数据
props
对象用于如上所述传递到组件中的数据 它应该是const{data}=this.props
而不是const data=this.props
这里有不止一个问题:你必须调用res.json()。然后…
而不是res.json())。然后…
,你试图设置状态
,但你正在读道具(const data=this.props
)等等,您有来自props的数据,或者这是一个错误?componentDidMount
中的承诺链也不正确。。。