简体   繁体   English

尝试在 React-Native 中使用 Hook 从 Api 渲染数据

[英]Try to render data from Api using Hook in React-Native

I'm new here and in react-native.我是新来的,并且是 React-native。

I want render some data from IMDB Api with the "hook" method.我想用“钩子”方法从 IMDB Api 渲染一些数据。 So I try to render data in a flatlist, I can see in the console that the data is retrieved properly in a jSon format witch is a good thing but when I open my app I don't see any rendering on the screen.所以我尝试在一个平面列表中渲染数据,我可以在控制台中看到数据以 json 格式正确检索,这是一件好事,但是当我打开我的应用程序时,我在屏幕上看不到任何渲染。

I try to use the 'Hook' method and not the 'classes' and I'm pretty sure that I don't use properly the useState method.我尝试使用“Hook”方法而不是“classes”,而且我很确定我没有正确使用 useState 方法。 So any help or advices are welcome.因此,欢迎任何帮助或建议。

My code:我的代码:

App应用程序

import React, { useState } from "react";
import {
  View,
  Text,
  StyleSheet,
  Button,
  TextInput,
  FlatList,
} from "react-native";
// import films from "../Helpers/filmsData";
import FilmsItem from "./FilmsItem";
import getFilmsFromApiWithSearchedText from "../Api/TMDBApi";

const Search = () => {
  const [films, setFilms] = useState([]);

  const _loadFilms = () => {
    getFilmsFromApiWithSearchedText("White").then(
      (data) => setFilms({ film: data.results }),
      console.log(films)
    );
  };

  return (
    <View style={styles.container}>
      <TextInput style={styles.input} placeholder="Titre du film" />
      <Button
        style={styles.button}
        title="Rechercher"
        onPress={() => _loadFilms()}
      />
      <FlatList
        data={setFilms.films}
        keyExtractor={(item) => item.id.toString()}
        renderItem={({ item }) => <FilmsItem films={item} />}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: 20,
  },
  input: {
    marginLeft: 5,
    marginRight: 5,
    height: 50,
    paddingLeft: 5,
    borderWidth: 1,
    borderColor: "#000000",
  },
});

export default Search;

Api request: api请求:

// Api/TMDBApi.js

const API_TOKEN = "**************************";

const getFilmsFromApiWithSearchedText = (text) => {
  const url =
    "https://api.themoviedb.org/3/search/movie?api_key=" +
    API_TOKEN +
    "&language=en&query=" +
    text;
  return fetch(url)
    .then((response) => response.json())
    .catch((error) => console.error(error));
};

export default getFilmsFromApiWithSearchedText;

FilmItem:电影项目:

import React from "react";
import { View, Text, StyleSheet, Image } from "react-native";

const FilmsItem = (props) => {
  const film = props.film;
  return (
    <View style={styles.container}>
      <Image style={styles.img} source={{ uri: "image" }} />
      <View style={styles.content}>
        <View style={styles.header}>
          <Text style={styles.title}>{film.title}</Text>
          <Text style={styles.vote}>{film.vote_average}</Text>
        </View>
        <View style={styles.synopsis}>
          <Text style={styles.synopsysText} numberOfLines={6}>
            {film.overview}
          </Text>
        </View>
        <View style={styles.date}>
          <Text style={styles.dateText}>Sorti le {film.release_date}</Text>
        </View>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flexDirection: "row",
    height: 190,
  },
  content: {
    flex: 1,
    margin: 5,
  },
  img: {
    height: 180,
    width: 120,
    margin: 5,
    backgroundColor: "gray",
  },
  header: {
    flex: 3,
    flexDirection: "row",
  },
  title: {
    flex: 1,
    flexWrap: "wrap",
    fontWeight: "bold",
    fontSize: 20,
    paddingRight: 5,
  },
  vote: {
    fontWeight: "bold",
    fontSize: 20,
    color: "#666666",
  },
  synopsis: {
    flex: 7,
  },
  synopsysText: {
    fontStyle: "italic",
    color: "#666666",
  },
  date: {
    flex: 1,
    paddingBottom: 10,
  },
  dateText: {
    textAlign: "right",
    fontSize: 14,
  },
});

export default FilmsItem;
const _loadFilms = () => {
    getFilmsFromApiWithSearchedText("White").then(
      (data) => setFilms({ film: data.results }),
      console.log(films)
    );
  };

setFilms({ film: data.results }) will set an object, but you need an array in there. setFilms({ film: data.results })将设置一个对象,但您需要一个数组。

Try to do something like this: setFilms(data.results)尝试做这样的事情: setFilms(data.results)

use useState in this way以这种方式使用useState

const _loadFilms = () => {
    getFilmsFromApiWithSearchedText("White").then(
      (data) => setFilms(data.results),
      console.log(films)
    );
  };

then in Flatlist you can use like this然后在Flatlist你可以这样使用

return (
    <View style={styles.container}>
      <TextInput style={styles.input} placeholder="Titre du film" />
      <Button
        style={styles.button}
        title="Rechercher"
        onPress={() => _loadFilms()}
      />
      <FlatList
        data={films}
        keyExtractor={(item) => item.id.toString()}
        renderItem={({ item }) => <FilmsItem films={item} />}
      />
    </View>
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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