[[oktatas:telefon:react_native|< React Native]] ====== Adat HTTP szerverről ====== * **Szerző:** Sallai András * Copyright (c) Sallai András, 2021, 2022 * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]] * Web: https://szit.hu ===== Online adatok ===== * https://reactnative.dev/movies.json * https://jsonplaceholder.typicode.com/users/ ===== REST API mókolás ===== { "cars": [ { "id": 1, "plate": "ADG-284", "color": "fehér", "brand": "Opel", "year": 1998, "capacity": 1400, "fuel": "benzin", "price": 260000, "sold": true }, { "id": 2, "plate": "GAD-912", "color": "piros", "brand": "Ford", "year": 2003, "capacity": 1400, "fuel": "benzin", "price": 370000, "sold": true }, { "id": 3, "plate": "FDE-102", "color": "fehér", "brand": "Ford", "year": 2004, "capacity": 1200, "fuel": "benzin", "price": 299000, "sold": false }, { "id": 4, "plate": "FLE-582", "color": "fekete", "brand": "Honda", "year": 2003, "capacity": 1400, "fuel": "benzin", "price": 579000, "sold": false }, { "id": 5, "plate": "FDN-132", "color": "kék", "brand": "Citroen", "year": 2006, "capacity": 1600, "fuel": "dízel", "price": 478000, "sold": false } ] } json-server indítása: json-server --host 0.0.0.0 --port 8000 ./cars.json --watch A telefon számára szükséges a --host kapcsoló használata. Vagy: * https://gist.githubusercontent.com/andteki/91c74e1ae7f292ec02f100e09fe52c7f/raw/487f4dd6e0e9da0bab6e8920b1c3b9f2c6b70e8f/test.json ===== Lekérés ===== import { StatusBar } from 'expo-status-bar'; import React from 'react'; import { StyleSheet, Text, View, FlatList, Button } from 'react-native'; import { useState, useEffect } from 'react'; export default function App() { const url1 = 'http://192.168.5.5:8000/cars'; const url2 = ''; const [cars, setCars] = useState([]); function handleFetchButton() { fetch(url1) .then(resp => resp.json()) .then(data => { setCars(data); }) .catch( error => { alert('Hiba: ' + error); }) } return ( Járművek