一: 组件的复用
二:获取后端给的数据并展示到页面上
import React, { Component } from 'react';
import axios from 'axios';
class Test extends Component {
constructor(props) {
super(props);
this.state = { seconds: 0 };
}
componentWillMount() {
let that = this;
axios.get('http:/')
.then(function (res) {
console.log(res);
let data = res.data;
let showapi_res_code = data.showapi_res_code;
that.setState({
seconds: showapi_res_code
})
})
.catch(function (error) {
console.log(error);
});
}
render() {
return (
<div className="App">
<h1>{this.state.seconds}</h1>
<button onClick={() => {this.add()}}>电控</button>
</div>
);
}
add(){
console.log(this.state.seconds)
let seconds = this.state.seconds + 1;
this.setState({
seconds: seconds
})
}
}
export default Test;