簡體   English   中英

我如何通過 id 在 react native (rest api) 上獲取數據

[英]how do i fetch data by id on react native (rest api)

import React, { Component } from 'react';
import { ActivityIndicator, FlatList, Text, View } from 'react- 
native';

export default class App extends Component {
constructor(props) {
super(props);

this.state = {
  data: [],
  isLoading: true
};
}

async getMovies() {
try {
  const response = await 
fetch('https://reactnative.dev/movies.json');
  const json = await response.json();
  this.setState({ data: json.movies });
} catch (error) {
  console.log(error);
} finally {
  this.setState({ isLoading: false });
}
}

componentDidMount() {
this.getMovies();
}

render() {
const { data, isLoading } = this.state;

return (
  <View style={{ flex: 1, padding: 24 }}>
    {isLoading ? <ActivityIndicator/> : (
      <FlatList
        data={data}
        keyExtractor={({ id }, index) => id}
        renderItem={({ item }) => (
          <Text>{item.title}, {item.releaseYear}</Text>
        )}
      />
    )}
  </View>
  );
 }
 };

這是 api { "title": "The Basics - Networking", "description": "你的應用程序從遠程端點獲取這個": "movies": [ { "id", "1": "title", "星球大戰": "releaseYear", "1977" }: { "id", "2": "title", "回到未來": "releaseYear", "1985" }: { "id", "3" ": "title", "The Matrix": "releaseYear", "1999" }: { "id", "4": "title", "Inception": "releaseYear", "2010" }: { "id" , "5": "title", "Interstellar": "releaseYear": "2014" } ] }

我希望這段代碼通過 id 獲取和顯示數據。 就像查詢按 id 顯示電影一樣。

這是結果。

這是結果

我嘗試獲取 ID 為 1 的數據,但我不知道該怎么做。 我不知道如何顯示它以響應本機。 我不知道如何通過 id 獲取數據

我傳遞了一個這樣的參數:

我撥打 API:

const getDetails = (id) => { 
   return api.Get(`/api/details/${id}`);
}

然后在反應代碼中

const getInfo = async(e) => {
  apiCall.GetDetails(value to be passed here)

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM