我第一次使用React时遇到了一些麻烦.我在我的项目中编写了一个简单的类:
import React, { Component } from 'react';
import axios from 'axios';
import logo from './logo.svg';
import './App.css';
class FigosTest extends React.Component {
state = {
provider: 'unknown',
date: 'sometime'
};
componentDidMount() {
axios.get('x.com/some_api')
.then(res => {
console.log(res.data.provider);
console.log(res.data.date);
this.setState(this.setState(res.data));
console.log("After");
console.log(this.state.provider);
console.log(this.state.date);
});
}
render() {
return this.provider || 'undefined';
}
}
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Result: <FigosTest/>
</p>
</header>
</div>
);
}
} …Run Code Online (Sandbox Code Playgroud)