【发布时间】:2018-08-18 21:09:55
【问题描述】:
大家好,我一直在尝试在 React 中创建一个使用工作人员 API 的 Rota,这是我一直在使用的代码。目前,它读取数组,然后每 3 秒向状态添加一个随机数。
我想要它做的是从 0 计数到数组的值,然后重置。有任何想法吗?
import React, { Component } from 'react';
import './App.css';
import axios from 'axios';
import { once, every, daily } from 'bella-scheduler';
class App extends Component {
constructor(props) {
super(props);
this.state = {
data: [0]
}
}
componentDidMount() {
axios.get('http://localhost:3333/staff')
.then((response) => {
this.successShow(response);
})
.catch((error) => {
this.successShow('error');
});
}
successShow(response) {
console.log(response.data);
every('3s', () => {
var i = Math.floor(Math.random()*(response.data.length))
this.setState({person: response.data[i].name});
console.log('Resolved task.');
});
}
render() {
return (
<div className="main">
<h2 className="title">Rota</h2>
<h3>{this.state.person}</h3>
</div>
);
}
}
export default App;
【问题讨论】:
标签: arrays reactjs api math state