所以我想每次點擊刷新按鈕時調用google finance API,並且stocks.stock應該更新。到目前爲止,我已經寫了下面的代碼,現在我不知道如何繼續下去。如何調用API並在React中填充響應
瀏覽器出現以下錯誤:未捕獲TypeError:e.currentTarget.data不是函數。
感謝
class App extends React.Component{
constructor(){
super();
this.state = {
stocks:[
{"ID":"1", "stock":"AAPL", "price": "$785.34",
"industry":"Tech"},
{"ID":"2", "stock":"WMT", "price": "$68.75",
"industry":"Tech"},
{"ID":"3", "stock":"AMZN", "price": "$810.45",
"industry":"Tech"},
{"ID":"4", "stock":"FB", "price": "$95.34",
"industry":"Tech"},
],
};
}
updateStock(e){
var stock = (e.currentTarget).data('stock');
var url=`https://www.google.com/finance/info?q=NASDAQ:${stock}`;
axios.get(url).then(response => {
var json = parser.parse(response.data);
let store = this.state.stocks;
store.price = json.l;
this.setState({
stocks: store
})
});
}
render(){
return (
<table>
<thead>
<tr>
<th> ID</th>
<th> stock</th>
<th> price</th>
<th> industry</th>
</tr>
</thead>
<tbody>
{
this.state.stocks.map((stocks) => {
return (
<tr>
<td>{stocks.ID}</td>
<td>{stocks.stock}</td>
<td>{stocks.price}</td>
<td>{stocks.industry}</td>
<td> <button onClick=
{this.updateStock}
data-stock=
{this.state.stocks.stock}>
refresh
</button>
</td>
</tr>
);
})
}
</tbody>
</table>
</div>
);
}
}
感謝,這有助於 –
不客氣,我很高興有沒有什麼幫助的話,我應該有一個給予好評和接受這個答案^^?如果還有任何問題,請隨時在這裏發佈錯誤日誌,我們會一起找到最好的方式,謝謝! – thinhvo0108