简体   繁体   中英

How can I make my React Native app go into the "fetch" method?

I have a question for people using React Native. Currently I'm trying to retrieve simple JSON data with the following url: https://jsonplaceholder.typicode.com/posts The problem I'm having is that I can't get my program to go through the "fetch method "although recognized by React Native, can someone help me?

My current React Native application with LOGS

在此处输入图片说明

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;

you can use axios to fetch data First install axios via npm i axios Then import axios ==> import axios from 'axios' This is an exemple:

useEffect(async () => {
const result = await axios(
  'https://jsonplaceholder.typicode.com/posts');

console.log(result.data);},[]);

我刚刚安装了另一台设备,它运行良好,再次感谢您的帮助,我很感激!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM