【问题标题】:how to access particular data in api call response in react native如何在本机反应中访问api调用响应中的特定数据
【发布时间】:2020-05-11 14:37:29
【问题描述】:

我正在使用 react native 并使用 moviesDB API。出于某种原因,我无法访问我在 api 调用响应中寻找的数据。我正在尝试从 api 调用中获取“poster_path”信息。因此,在我的 console.log 中,如果调用 this.state.movi​​es,我会在那里看到许多电影的数据以及我想要访问的“poster_path”键及其信息。但是,当我 console.log this.state.movi​​es.poster_path 时,它显示未定义。只是想知道它为什么这样做。我曾尝试在线搜索答案,并尝试将括号括起来并取出扩展运算符。请参阅下面的控制台代码和图片。谢谢!

import { View, Text, StyleSheet, TextInput, Image } from "react-native";
import { FlatList, TouchableOpacity } from "react-native-gesture-handler";
import MovieItem from "../components/MovieItem";

const API_KEY2="*******";

class SearchScreen extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            movies: \[\],
            searchTerm: "",
        }
    }

    handleSubmit =(e)=> {

        fetch(`https://api.themoviedb.org/3/search/movie?api_key=${API_KEY2}&query=${this.state.searchTerm}`)
            .then(data => data.json())
            .then(data=> {
                this.setState({
                    movies: \[...data.results\]
                });
                console.log("RESPONSE FROM THIS.STATE.MOVIES", this.state.movies)
                console.log("RESPONSE FROM THIS.STATE.MOVIES.POSTER_PATH",this.state.movies.poster_path)

            }) 

    }

    handleChange=(textToSearch)=> {
        this.setState({
            searchTerm: textToSearch
        });
    }

    render() {


        return(
            <View style={styles.screen}>
                <TextInput 
                    style={styles.input}
                    onSubmitEditing={this.handleSubmit} 
                    onChangeText={(text)=>this.handleChange(text)} 
                    placeholder="Enter Movie" 
                    />
                    <FlatList 
                        data={this.state.movies} 
                        renderItem={({item})=> {
                            return(
                                <TouchableOpacity onPress={()=> this.props.navigation.navigate("MovieItem", {item})}>
                                    <View style={styles.movieItem}>
                                    <Image source={{uri:`https://image.tmdb.org/t/p/w1280/${item.poster_path}`}} 
                                        style={{
                                            height: 220, 
                                            width: 200
                                        }}/>
                                            <MovieItem item={item}/>
                                    </View>
                                </TouchableOpacity>
                                )
                    }} />
            </View>
        )
    }
}


const styles = StyleSheet.create({
    screen: {
        flex: 1,
        backgroundColor:"tomato",
        justifyContent:"center",
        alignItems:"center",
        flexDirection:"column"
    },
    input: {
        borderStyle:"solid", 
        borderWidth: 5, 
        width:"100%", 
        padding: 20,
        backgroundColor:"white",
        fontFamily:"Yesteryear-Regular",
        fontSize: 20,
        color:"tomato"
    },
    movieItem: {
        marginTop: 20,
        marginBottom: 20
    },

})


export default SearchScreen;][1]][1]

// this.state.movi​​es 的响应 // this.state.movi​​es.poster_path 的响应

【问题讨论】:

  • this.state.movi​​es[0].poster_path,
  • 1. this.setState 是异步的,所以更改后直接记录状态是行不通的(会显示之前的状态) 2.它是一个数组,所以你需要在访问属性之前指定一个元素
  • 我试过这样做 this.state.movi​​es[0].poster_path 但只返回一个海报路径。在下面的代码中,我试图获取用于平面列表组件的多个数据。有什么办法吗?
  • 你能分享一下你在`FlatList函数中写了什么
  • 当然,我的 flatlist 在 FlatList 组件的上述代码中。我也尝试在这里写它,但它说太长了

标签: javascript reactjs database api react-native


【解决方案1】:

var temp = [{'poster_path' :'xxx'}];

temp[0].poster_path;

【讨论】:

  • 无论如何访问数组中的所有poster_path?
  • 你必须使用 .map 然后循环遍历所有数据。
猜你喜欢
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
相关资源
最近更新 更多