Python 3.x 从api获取值并显示在reactjs应用程序图表中
我从返回值的地方创建了API,现在尝试获取这些值并使用reactjs显示在饼图中,但无法获取Python 3.x 从api获取值并显示在reactjs应用程序图表中,python-3.x,reactjs,api,Python 3.x,Reactjs,Api,我从返回值的地方创建了API,现在尝试获取这些值并使用reactjs显示在饼图中,但无法获取 class Graph extends Component { ComponentWillMount(){ fetch('http://localhost:3010/analysis') .then(response{response.json()}) .then(data => console.log(response)) }
class Graph extends Component {
ComponentWillMount(){
fetch('http://localhost:3010/analysis')
.then(response{response.json()})
.then(data => console.log(response))
}
render() {
return (
<ReactFC
{...pieConfigs}/>
);
}
}
export default Graph;
这是我的密码。有人能告诉我需要做哪些更改才能在react应用程序中显示饼图吗?问题是您在卸载组件后立即获取API,请重试
ComponentDidMount(){
fetch('http://localhost:3010/analysis')
.then(response{response.json()})
.then(data => console.log(response))
}
例如:
class Graph extends Component {
state = { pieConfigs }
componentDidMount(){
fetch('http://localhost:3010/analysis')
.then(response => response.json())
.then(data => this.setState({ data }))
}
render() {
return (
<ReactFC {...this.state.pieConfigs}/>
);
}
}
说明:
1.正在从componentDidMount而不是componentWillMount获取数据。您可以查看作为参考。
2.对与ui相关的数据使用状态/道具。您可以使用state和setState更新数据,并将其作为道具传递给ReactFC。更新状态或道具将触发重新呈现,这样ui将是最新的。如果您想了解如何配置图表,还可以在componentDidMount中调用API,而不是console.log,将数据设置为可用于图表配置的状态。