[英]React Native - How to make a variable global from my fetch method
[英]How can I make my React Native app go into the "fetch" method?
我有一個問題要問使用 React Native 的人。 目前我正在嘗試使用以下 url 檢索簡單的 JSON 數據: https : //jsonplaceholder.typicode.com/posts我遇到的問題是我無法讓我的程序通過“獲取方法”雖然被 React Native 認可,有人可以幫助我嗎?
我當前使用 LOGS 的 React Native 應用程序
import React, { useEffect } from 'react';
import { Text, View } from 'react-native';
const App = () => {
var url = "https://jsonplaceholder.typicode.com/posts";
useEffect(() => {
console.log("useEffect")
const result = fetch(url)
.then((response) => {
console.log(response.json) // should display the data in the logs
})
.then((json) => {
console.log(json);
});
console.log(result);
})
return(
<View>
<Text>Mon test</Text>
</View>
);
}
export default App;
你可以使用 axios 來獲取數據 首先通過npm i axios
安裝 axios 然后 import axios ==> import axios from 'axios'
這是一個例子:
useEffect(async () => {
const result = await axios(
'https://jsonplaceholder.typicode.com/posts');
console.log(result.data);},[]);
我剛剛安裝了另一台設備,它運行良好,再次感謝您的幫助,我很感激!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.