React native 在React Native中,我可以设置api调用中的对象的状态吗?

React native 在React Native中,我可以设置api调用中的对象的状态吗?,react-native,api,setstate,React Native,Api,Setstate,这应该是一件非常简单的事情,但我在这件事上真的很努力。我要做的就是从api调用中将id的状态设置为“results.id”。一旦它改变了api内部的状态,我就能够成功地打开filmography api。我已经通过输入一个真实的id测试了axios fetch url,它可以正常工作。因此,我基本上是试图获取从搜索中获取的id,并用该id更新id状态。如果我试图在错误的函数中设置状态,那么请务必帮助我在正确的函数中获取它!(我也知道我有一些草率的代码,但在我准备好永久保存之前,很多代码都是我的个

这应该是一件非常简单的事情,但我在这件事上真的很努力。我要做的就是从api调用中将id的状态设置为“results.id”。一旦它改变了api内部的状态,我就能够成功地打开filmography api。我已经通过输入一个真实的id测试了axios fetch url,它可以正常工作。因此,我基本上是试图获取从搜索中获取的id,并用该id更新id状态。如果我试图在错误的函数中设置状态,那么请务必帮助我在正确的函数中获取它!(我也知道我有一些草率的代码,但在我准备好永久保存之前,很多代码都是我的个人笔记)

import React,{useState}来自“React”;
进口{
看法
文本输入,
按钮
文本,
活动指示器,
滚动视图,
形象,,
触控高光,
警觉的,
}从“反应本族语”;
从“反应本机模态”导入模态;
从“axios”导入axios;
导出默认函数屏幕4(){
//id是特定参与者常量APIRL5=”的2位数字http://api.tmdb.org/3/search/person?api_key=84c329a92566be57845322a19ff707ac&query=“const apirl4=“/movie_credits?api_key=84c329a92566be5784322a19ff707ac&language=en-US”const apirl3=”https://api.themoviedb.org/3/person/“const apirl2="https://api.themoviedb.org/3/movie/upcoming?api_key=84c329a92566be57845322a19ff707ac&language=en-US&page=1“const apiurl=”http://www.omdbapi.com/?apikey=7ad73765&“const[state,setState]=useState({s:“输入一个参与者…”,id:,结果:[],选中:[],modalVisible:false,modalVisible2:false});
常量搜索=()=>{
//APIRL+“&t=“+state.s(单个结果)
//APIRL+“&s=“+state.s(多个结果)
axios(apirl5+state.s)。然后({data})=>{
//让结果=[数据];-----*******将此用于&t=*******************-------------
//让results=data.Search;-----*******使用此for&s=*******************-------------
让结果=data.results;
设id=state.id;
设置状态((prevState)=>{
返回{…prevState,modalVisible:true};
}),
设置状态((prevState)=>{
返回{…prevState,结果:results};
}),
设置状态((prevState)=>{
返回{…prevState,id:id};
}),
Alert.Alert(“ID为:”,ID[
{text:“Close”,onPress:()=>console.log(“警报关闭”)},
]);
});
};
常量openPopup=()=>{
axios(apirl3+state.id+apirl4)。然后({data})=>{
让结果=data.cast;
设置状态((prevState)=>{
返回{…prevState,modalVisible2:true};
}),
设置状态((prevState)=>{
返回{…prevState,选中:结果};
});
});
};
返回(
电影大师!
设置状态((prevState)=>{
返回{…prevState,s:text};
})
}
onSubmitEditing={search}
值={state.s}
/>
{/*key=result.imdbID-
这将提供多个URL中带有&s=的搜索结果
键=结果-
这将在URL*/}中给出带有&t=is的结果
{state.results.map((结果,索引)=>(
性别:{results.Gender}
ID:{results.ID}
{results.known_for.map((k,i)=>(
openPopup()}
>
标题:{k.Title}
))}
{/*标题:{results.gender}--这同样有效------*/}
{/*{dataItems.map((项,索引)=>(
{item.title}
{item.content.map((c,i)=>(
{c.title}
{c.description}

))} ))} */} ))} 设置状态((prevState)=>{ 返回{…prevState,modalVisible:false}; }) } 风格={{ 玛金托普:50, 颜色:“红色”, 尺寸:40, fontWeight:“粗体”, }} > 接近! {/*模态中的animationType可以是淡入淡出、无或滑动*/} {state.selected.map((cast,index2)=>( 标题:{cast.Title} 概述:{cast.Overview} ))} 设置状态((prevState)=>{ 返回{…prevState,modalVisible2:false}; }) } > 接近! ); }
用于results.id的API:

电影摄影API:


附加了一个图像,显示了我试图在

中设置的ID。我找到了它。我必须使用一个for循环来获取我需要的数据,然后才能设置该数据。起初我不清楚的是,这是否有必要,如果有必要,我假设我必须在我映射东西的代码部分这样做。但是没有,o我在搜索函数中得到了一个for循环,这对我来说开始有意义了

import React, { useState } from "react";
import {
  View,
  TextInput,
  Button,
  Text,
  ActivityIndicator,
  ScrollView,
  Image,
  TouchableHighlight,
  Alert,
} from "react-native";
import Modal from "react-native-modal";
import axios from "axios";

export default function Screen4() {
  // id is a 2 digit number for specific actor const apiurl5 = "http://api.tmdb.org/3/search/person?api_key=84c329a92566be57845322a19ff707ac&query=" const apiurl4 = "/movie_credits?api_key=84c329a92566be57845322a19ff707ac&language=en-US" const apiurl3 = "https://api.themoviedb.org/3/person/" const apiurl2 = "https://api.themoviedb.org/3/movie/upcoming?api_key=84c329a92566be57845322a19ff707ac&language=en-US&page=1"; const apiurl = "http://www.omdbapi.com/?apikey=7ad73765&"; const [state, setState] = useState({ s: "Enter an actor...", id: "", results: [], selected: [], modalVisible: false, modalVisible2: false });

  const search = () => {
    // apiurl + "&t=" + state.s (Single Result)
    // apiurl + "&s=" + state.s (Multiple Results)
    axios(apiurl5 + state.s).then(({ data }) => {
      //let results = [data]; ----- ******** Use this for &t= **************** -------------
      //let results = data.Search; ----- ******** Use this for &s= **************** -------------
      let results = data.results;
      let id = state.id;

      setState((prevState) => {
        return { ...prevState, modalVisible: true };
      }),
        setState((prevState) => {
          return { ...prevState, results: results };
        }),
        setState((prevState) => {
          return { ...prevState, id: id };
        }),
        Alert.alert("The ID is: ", id, [
          { text: "Close", onPress: () => console.log("alert closed") },
        ]);
    });
  };

  const openPopup = () => {
    axios(apiurl3 + state.id + apiurl4).then(({ data }) => {
      let result = data.cast;

      setState((prevState) => {
        return { ...prevState, modalVisible2: true };
      }),
        setState((prevState) => {
          return { ...prevState, selected: result };
        });
    });
  };

  return (
    <View style={{ flex: 1, padding: 10, justifyContent: "center" }}>
      <Text>Cinemaster!</Text>
      <TextInput
        style={{
          borderBottomWidth: 1,
          borderBottomColor: "#ff0000",
          marginBottom: 20,
        }}
        onChangeText={(text) =>
          setState((prevState) => {
            return { ...prevState, s: text };
          })
        }
        onSubmitEditing={search}
        value={state.s}
      />
      <Button onPress={search} title="Search"></Button>
      {/* key=result.imdbID - 
                                    This gives multiple search results with the &s= is in the URL

                                    key=result -
                                    This gives the result with the &t= is in the URL */}
      <Modal
        //animationType="slide"
        transparent={false}
        //visible={(state.modalVisible)}
        animationIn="slideInRight"
        animationOut="slideOutLeft"
        useNativeDriver={true}
        isVisible={state.modalVisible}
      >
        <ScrollView>
          {state.results.map((results, index) => (
            <TouchableHighlight key={index}>
              <View style={{ flex: 1, padding: 10, justifyContent: "center" }}>
                <Button title="Full Filmography" onPress={openPopup}></Button>

                <Text>Gender: {results.gender}</Text>
                <Text>ID: {results.id}</Text>

                {results.known_for.map((k, i) => (
                  <TouchableHighlight
                    key={i}
                    // onPress={() => openPopup()}
                  >
                    <View>
                      <Text>Title: {k.title}</Text>
                      <Image
                        source={{
                          uri:
                            "https://image.tmdb.org/t/p/original/" +
                            k.poster_path,
                        }}
                        style={{ width: 300, height: 500 }}
                        resizeMode="cover"
                      />
                    </View>
                  </TouchableHighlight>
                ))}

                {/* <Text>Title: {results.gender}</Text> -----THIS ALSO WORKS----- */}

                {/* {dataItems.map((item, index) => (
                                    <div key={index}>
                                        <h1>{item.title}</h1>
                                        {item.content.map((c, i) => (
                                        <div key={i}>
                                            <img src={c.imageUrl} />
                                            <h3>{c.title}</h3>
                                            <h3>{c.description}</h3>
                                            <hr />
                                        </div>
                                        ))}
                                    </div>
                                    ))} */}
              </View>
            </TouchableHighlight>
          ))}

          <Text
            onPress={() =>
              setState((prevState) => {
                return { ...prevState, modalVisible: false };
              })
            }
            style={{
              marginTop: 50,
              color: "red",
              fontSize: 40,
              fontWeight: "bold",
            }}
          >
            Close!
          </Text>
        </ScrollView>
      </Modal>

      {/* animationType in Modal can be fade, none, or slide */}
      <Modal
        //animationType="slide"
        transparent={false}
        //visible={(state.modalVisible)}
        animationIn="slideInRight"
        animationOut="slideOutLeft"
        useNativeDriver={true}
        isVisible={state.modalVisible2}
      >
        <ScrollView>
          {state.selected.map((cast, index2) => (
            <View key={index2}>
              <Text>Title:{cast.title} </Text>
              <Text>Overview:{cast.overview} </Text>
            </View>
          ))}
        </ScrollView>
        <TouchableHighlight
          onPress={() =>
            setState((prevState) => {
              return { ...prevState, modalVisible2: false };
            })
          }
        >
          <Text
            style={{
              marginTop: 50,
              color: "red",
              fontSize: 40,
              fontWeight: "bold",
            }}
          >
            Close!
          </Text>
        </TouchableHighlight>
      </Modal>
    </View>
  );
}