日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Java 接受reactjs数据_ReactJS:从API获取数据

發布時間:2025/3/12 javascript 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 接受reactjs数据_ReactJS:从API获取数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我在使用簡單的標準模板獲取API數據的React應用程序中從API獲取數據時遇到了困難 . 控制臺日志返回的結果是空白數組 .

import React, {Component} from 'react';

import './App.css';

import Chart from './components/chart'

const API_URL = "http://ergast.com/api/f1/2016/1/results.json";

class App extends Component {

constructor(props) {

super(props)

this.state = {

results: [],

};

}

componentDidMount() {

fetch(API_URL)

.then(response => {

if (response.ok) {

return response.json()

}

else {

throw new Error ('something went wrong')

}

})

.then(response => this.setState({

results: response.MRData

})

)}

render() {

const {results} = this.state;

return (

);

}

}

export default App;

chart.js

import React from 'react';

import {XYPlot, XAxis, YAxis, VerticalGridLines, HorizontalGridLines, LineSeries} from 'react-vis';

const Chart = (props) => {

console.log(props.data);

const dataArr = props.data.map((d)=> {

return {x: d.RaceTable.Results.Driver.driverId, y: d.RaceTable.Results.position}

});

console.log(dataArr);

return (

xType="ordinal"

width={1000}

height={500}>

data={dataArr}

style={{stroke: 'violet', strokeWidth: 3}}/>

);

}

export default Chart;

真的無法弄清楚我哪里出錯了 . 我用 results: response.MRData 正確設置了狀態不是嗎? (MRData是JSON的關鍵 . )這是json的結構 . API鏈接:http://ergast.com/api/f1/2016/1/results.json

JSON Example Response

{

"MRData": {

"xmlns": "http://ergast.com/mrd/1.0",

"RaceTable": {

"Races": [

{

"season": "2008",

"round": "1",

}

},

"Results": [

{

"position": "1",

"Driver": {

"driverId": "hamilton",

"permanentNumber": "44",

"code": "HAM",

"url": "http://en.wikipedia.org/wiki/Lewis_Hamilton",

"givenName": "Lewis",

"familyName": "Hamilton",

"dateOfBirth": "1985-01-07",

"nationality": "British"

},

}

},

.

.

.

]

}

]

}

}

}

總結

以上是生活随笔為你收集整理的Java 接受reactjs数据_ReactJS:从API获取数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。