![](/img/trans.png)
[英]React Native: How to get data from JSON with different keys and display it?
[英]How to display data from json in React
我嘗試創建生成隨機報價的簡單應用程序。 我創建了一個函數,(我認為)從 json 文件中獲取我想要的數據。 但是當我嘗試將該函數傳遞給我的 App 函數時,我得到錯誤:對象作為 React 子對象無效(發現:[object Promise])
功能報價:
function Quote (data) {
var x = (Math.floor(Math.random() * (103 - 1) + 1) );
return fetch('https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json')
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson['quotes'][0]['author']);
return responseJson['quotes'][x]['author'];
})
.catch((error) => {
console.error(error);
});
}
應用功能:
function App() {
var text = '';
return (
<div id="quote-box">
<div id="author"><Quote /></div>
<button id="new-quote">New Quote</button>
<a href="twitter.com" id="tweet-quote">Tweet</a>
</div>
);
}
我會使用 useEffect 在開始時觸發調用。 並使用 useState 來保存值。 然后還將相同的邏輯添加到 onClick。
import { useEffect, useState } from "react";
function getQuote() {
var x = Math.floor(Math.random() * (103 - 1) + 1);
return fetch(
"https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json"
)
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson["quotes"][0]["author"]);
return responseJson["quotes"][x]["author"];
})
.catch((error) => {
console.error(error);
});
}
export default function App() {
const [author, setAuthor] = useState("");
useEffect(() => {
getQuote().then((newAuthor) => setAuthor(newAuthor));
}, []);
return (
<div id="quote-box">
<div id="author">{author}</div>
<button
id="new-quote"
onClick={() => getQuote().then((newAuthor) => setAuthor(newAuthor))}
>
New Quote
</button>
<a href="twitter.com" id="tweet-quote">
Tweet
</a>
</div>
)
}
您也可以通過這種方式存檔。 創建自定義掛鈎並使用它。
import React from "react";
function useFetchQuote(newQuote) {
const [author, setAuthor] = React.useState();
React.useEffect(() => {
var x = Math.floor(Math.random() * (20 - 1) + 1);
return fetch(
"https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json"
)
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson["quotes"][x]["author"]);
setAuthor(responseJson["quotes"][x]["author"]);
})
.catch((error) => {
console.error(error);
});
}, [newQuote]);
return { author };
}
function App() {
const [newQuote, setQuote] = React.useState(0);
const { author } = useFetchQuote(newQuote);
return (
<div id="quote-box">
<div id="author">{author}</div>
<button id="new-quote" onClick={() => setQuote(newQuote + 1)}>
New Quote
</button>
<a href="twitter.com" id="tweet-quote">
Tweet
</a>
</div>
);
}
export default App;
在React
組件中返回 promise 不會像您在代碼中所做的那樣工作。 React
組件必須返回一個jsx
。 例如在名為Quote.js
的文件中
import * as React from 'react';
const url =
'https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json';
const Qoute = () => {
const [quote, setQuote] = React.useState(null);
React.useEffect(() => {
fetch(url)
.then((response) => response.json())
.then((data) => {
const randomNumber = 1; // Generate random number which is lesser or equal to data's length
setQuote(data['quotes'][randomNumber]);
});
}, []);
if (!quote) return <React.Fragment>Loading...</React.Fragment>;
return <div>{JSON.stringify(quote)}</div>;
};
export default Qoute;
然后你只需要將它導入到你想使用它的地方並像這樣調用它
<Quote />
PS:如果某些東西不起作用,我會從typescript
轉換它,我很樂意提供幫助。 並請記得更新這里我把注釋行。 祝你好運,兄弟。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.