【发布时间】:2021-09-10 06:00:39
【问题描述】:
enter image description here我是 react-native 的新手。我试图使用 fetch 访问 API。我能够在控制台上打印结果,但无法在移动屏幕上打印。我附上了下面练习代码的截图。 [在此处输入图片描述][2]
import React from 'react'
import { StyleSheet, Text, SafeAreaView} from 'react-native'
const getMoviesFromApi = () => {
//GET request
fetch('https://reactnative.dev/movies.json', {
method: 'GET',
//Request Type
//method: 'POST',
})
.then((response) => response.json())
//If response is in json then in success
.then((responseJson) => {
//Success
// alert(JSON.stringify(responseJson));
//console.log(responseJson);
return responseJson;
})
}
const App = () => {
let data = getMoviesFromApi()
console.log(data)
return (
<SafeAreaView>
<Text> JSON.stringify{data} </Text>
{/* <Text> JSON.stringify{parseData} </Text> */}
</SafeAreaView>
)
}
const styles = StyleSheet.create({})
export default App
【问题讨论】:
标签: javascript reactjs react-native