【发布时间】:2020-06-12 04:50:22
【问题描述】:
我对反应非常陌生,我正在尝试使用 Axios 从 API 获取人口数据并显示改变世界的加班人口。像这样的。
我正在尝试使用chart-race-react 构建它。存储库中只有一个示例,因此我尝试从该示例中获取提示。
我试过了:
import React, { Component } from 'react';
import BarChart from 'chart-race-react';
import axios from 'axios';
import './App.css';
const API_URL = 'https://example.json';
class App extends Component {
state = {
countries: []
}
componentDidMount() {
axios.get(API_URL)
.then(response => response.data)
.then((data) => {
this.setState({ countries: data })
console.log(this.state.countries)
})
}
render() {
return (
<div style={{width: "500px"}}>
<BarChart
start={true}
data = {this.state.countries}
len = {this.state.countries.length}
timeout={400}
delay={100}
colors = "rgb(133, 131, 131)"
timeline = {Array(20).fill(0).map((itm, idx) => idx + 1)}
timelineStyle={{
textAlign: "center",
fontSize: "50px",
color: "rgb(148, 148, 148)",
marginBottom: "100px"
}}
textBoxStyle={{
textAlign: "right",
color: "rgb(133, 131, 131)",
fontSize: "30px",
}}
barStyle={{
height: "60px",
marginTop: "10px",
borderRadius: "10px",
}}
width={[15, 75, 10]}
maxItems = {this.state.countries.length}
/>
</div>
);
}
}
export default App;
我得到一个输出,其中一个数字出现在浏览器中,来自1 to 20。我怎样才能让我的应用看起来完全像上面的gif?我在这里做错了什么?
【问题讨论】:
-
1.您不能只发布多行错误的 1 行并希望得到帮助。请发布整个堆栈跟踪。 2. 很明显,无论代码来自哪里,它都不存在于您发布的代码中,因为您甚至没有提到变量
colors。这有点让我回到“发布完整的堆栈跟踪和所有相关代码” -
如果我不清楚,我很抱歉。所以基本上我正在使用这个npm package 包。请访问,看看您是否可以提供帮助
-
不错的选择。 NPM 包看起来不错,但您仍然需要发布完整的堆栈跟踪。简单的
_this.props.colors is undefined是不够的。您仍然无法从中分辨出是哪一行代码导致了错误,如果错误是由库引发的,那么在错误发生之前最后一次触发了哪一行代码。堆栈跟踪包含此类详细信息,我假设它存在于您的日志中......或者_this.props.colors is undefined是整个错误并且控制台日志中实际上没有更多内容? -
我添加了错误
标签: javascript reactjs react-chartjs