简体   繁体   English

React Native 将电影的 api 数据显示到屏幕上

[英]React Native display the api data of movies to screen

Hello i'm trying to display the api of these movies but they wont show on my phone but when i console log they work in my terminal, just having issues displaying the results to the device.您好,我正在尝试显示这些电影的 API,但它们不会显示在我的手机上,但是当我控制台登录时,它们在我的终端中工作,只是在向设备显示结果时出现问题。

At first i wast giving a error of起初我是在给一个错误

TypeError undefined is not an object {evaluating 'state.results.map'} TypeError undefined 不是一个对象 {evaluating 'state.results.map'}

here is the code below这是下面的代码

import React ,{ useState }from 'react';
import axios from'axios';
import { StyleSheet, Text, View ,ScrollView,TextInput} from 'react-native';

export default function App() {
  const apiurl = "http://www.omdbapi.com/?i=tt3896198&apikey=4c0e830";
  const [state,setState] = useState({
    s:'Enter A Movie...',
    results:[],
    selected:{}
  });

  const search = () => {
    axios(apiurl + "&s=" + state.s).then(({data})=>{
      let results = data.Search
      console.log(results)
      setState(prevState =>{
        return {...prevState,results:results}
      })
    })
  }

  return (
    <View style={styles.container}>
      <Text style={styles.Header}>Movie DataBase</Text>
      <TextInput 
      style={styles.searchBar}
      onChangeText={text => setState(prevState =>{
        return{...prevState, s: text}
      })}
      onSubmitEditing={search}
      value = {state.s}

       />
       <ScrollView style={styles.results}>
       {state.results.map(result => {
         <View key={result.imdbID} style={styles.result}>
         <Text>{result.Title}</Text>

         </View>
       })}



       </ScrollView>
    </View>
  );
}

try add something like:尝试添加如下内容:

{state.results && state.results.map (...)

or:或者:

{state.results.length > 0 && state.results.map (...)

or:或者:

{state.results !== undefined && state.results.map (...)

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

相关问题 为什么 firebase 数据没有通过在 react native 中使用 flatlist 显示在屏幕上 - why firebase data is not display on screen by using flatlist in react native 如何在React Native中按ID显示来自API的数据 - how to display data from api by id in react native 如何从 API 中过滤特定数据并将其显示在 React Native Flatlist 中? - How to filter specific data from API and display it in React Native Flatlist? 在反应原生表组件中显示从 API 获取的数据 - Display Fetched Data from API in react native table component 如何通过发布请求 api 在 ListView 上显示 json 数据反应本机 - How to display json data on ListView react native by post request api 如何在 React Native 中水平显示来自 API 的数据 - How to display data coming from an API horizontally in react native React Native:我可以在控制台上打印我的数据,但是如何在我的 React Native 应用程序屏幕上显示数据? - React Native: I am able to print my data on my console, but how do i display the data on my React Native app screen? 如何在React Native中在屏幕中间显示图标 - How to display Icon in middle of screen in react native Flatlist React Native-无数据显示 - Flatlist React Native - No Data Display React 中 OMDb api 上每页 30 部电影 - 30 movies per page on OMDb api in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM