为什么每当我使用React Navigation,usafeft和Axios导航到详细信息屏幕时,它显示了以前的数据

发布于 2025-02-13 07:48:07 字数 2253 浏览 0 评论 0原文

我正在尝试使用Expo构建电影详细信息React Antive App,并且每当我试图从家中导航到详细信息页面时,每当我在电影详细信息页面上显示以前的数据时。如何解决这个问题。 我正在使用React Navigation在屏幕之间导航,并且我也使用Axios获取来自API的数据。 我不明白,为什么每次显示先前的数据时。

视频 - https://www.youtube.com/shorts/0jgl2spyujw

- https://i.sstatic.net/0qle1.png

const DetailsScreen = ({ route, navigation }) => {
    const [apis, setApis] = useState([]);
    const URL = "https://demoapi.com" + route.params.categoryWithId;
    useEffect(() => {
        console.log("Top ==>" + URL);
        async function getAllData() {
            try {
                const apis = await axios.get(URL);
                setApis(apis.data);
            } catch (error) {
                console.log(error);
            }
        }
        getAllData();
    }, []);

    if (!apis.length) return null;
    console.log("details ==> " + JSON.stringify(apis));

    return (
        <>
            <ImageBackground source={{ uri: apis[0].image }} resizeMode="cover" style={styles.backgroundImage} >
                <Wrapper>
                    {/* PlatformLogo */}
                    <ImageBackground
                        resizeMode="contain"
                        source={{
                            uri: "https://pngimg.com/uploads/netflix/netflix_PNG32.png",
                        }}
                        style={styles.platformLogo}
                    />
                    <Title>{apis[0].title}</Title>
                    {/* <Text>{verticlebanner}</Text> */}
                    <SubTitle>{apis[0].date}</SubTitle>
                    <PlayIcon
                        style={{
                            backgroundColor: "white",
                            borderRadius: 50,
                        }}
                    />
                </Wrapper>
            </ImageBackground>

            <StatusBar hidden />
        </>
    );
};

屏幕显示“ Dr. Strange”,然后我单击“壁架”,然后显示“ Minions”的数据等... 为什么它每次都显示以前的数据

I am trying to build a movie details react native app with expo and whenever I'm trying to navigate from home to details page, every time it shows me previous data on movie details page. How to solve this issue.
I am using react navigation to navigate between screens and also I'm using axios to get data from and api.
I didn't understand, why every time it shows previous data.

Preview in video- https://www.youtube.com/shorts/0JgL2SPyujw

DetailsScreen.js- https://i.sstatic.net/0qLE1.png

const DetailsScreen = ({ route, navigation }) => {
    const [apis, setApis] = useState([]);
    const URL = "https://demoapi.com" + route.params.categoryWithId;
    useEffect(() => {
        console.log("Top ==>" + URL);
        async function getAllData() {
            try {
                const apis = await axios.get(URL);
                setApis(apis.data);
            } catch (error) {
                console.log(error);
            }
        }
        getAllData();
    }, []);

    if (!apis.length) return null;
    console.log("details ==> " + JSON.stringify(apis));

    return (
        <>
            <ImageBackground source={{ uri: apis[0].image }} resizeMode="cover" style={styles.backgroundImage} >
                <Wrapper>
                    {/* PlatformLogo */}
                    <ImageBackground
                        resizeMode="contain"
                        source={{
                            uri: "https://pngimg.com/uploads/netflix/netflix_PNG32.png",
                        }}
                        style={styles.platformLogo}
                    />
                    <Title>{apis[0].title}</Title>
                    {/* <Text>{verticlebanner}</Text> */}
                    <SubTitle>{apis[0].date}</SubTitle>
                    <PlayIcon
                        style={{
                            backgroundColor: "white",
                            borderRadius: 50,
                        }}
                    />
                </Wrapper>
            </ImageBackground>

            <StatusBar hidden />
        </>
    );
};

when I click on "minions" then details screens shows "dr. strange" and I click on "the ledge" then it shows data of "minions" and so on...
why it shows previous data every times

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2025-02-20 07:48:07

根据您的代码,仅需要lodader或isloading状态以进行API调用和状态更新,如下所示,代码对我来说很好。

const DetailsScreen = ({ route, navigation }) => {
const [apis, setApis] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const URL = "https://demoapi.com" + route.params.categoryWithId;
useEffect(() => {
  console.log("Top ==>" + URL);
  getAllData();
}, []);
const getAllData = async () => {
try {
  const apis = await axios.get(URL);
  setApis(apis.data);
} catch (error) {
  console.error(error);
 } finally {
   setIsLoading(false);
 }
 }
  if (!apis.length) return null;
console.log("details ==> " + JSON.stringify(apis));
 return (
<>
  {isLoading ? <ActivityIndicator size={'large'} color={'#113A69 '} />
    : <>
      <ImageBackground source={{ uri: apis[0].image }} resizeMode="cover" style={styles.backgroundImage} >
        <Wrapper>
          {/* PlatformLogo */}
          <ImageBackground
            resizeMode="contain"
            source={{
              uri: "https://pngimg.com/uploads/netflix/netflix_PNG32.png",
            }}
            style={styles.platformLogo}
          />
          <Title>{apis[0].title}</Title>
          {/* <Text>{verticlebanner}</Text> */}
          <SubTitle>{apis[0].date}</SubTitle>
          <PlayIcon
            style={{
              backgroundColor: "white",
              borderRadius: 50,
            }}
          />
        </Wrapper>
      </ImageBackground>
      <StatusBar hidden />
    </>
  }
</>
);
 };

As per your code it only required lodader or isLoading state for api call and state update as shown in below code it works fine for me.

const DetailsScreen = ({ route, navigation }) => {
const [apis, setApis] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const URL = "https://demoapi.com" + route.params.categoryWithId;
useEffect(() => {
  console.log("Top ==>" + URL);
  getAllData();
}, []);
const getAllData = async () => {
try {
  const apis = await axios.get(URL);
  setApis(apis.data);
} catch (error) {
  console.error(error);
 } finally {
   setIsLoading(false);
 }
 }
  if (!apis.length) return null;
console.log("details ==> " + JSON.stringify(apis));
 return (
<>
  {isLoading ? <ActivityIndicator size={'large'} color={'#113A69 '} />
    : <>
      <ImageBackground source={{ uri: apis[0].image }} resizeMode="cover" style={styles.backgroundImage} >
        <Wrapper>
          {/* PlatformLogo */}
          <ImageBackground
            resizeMode="contain"
            source={{
              uri: "https://pngimg.com/uploads/netflix/netflix_PNG32.png",
            }}
            style={styles.platformLogo}
          />
          <Title>{apis[0].title}</Title>
          {/* <Text>{verticlebanner}</Text> */}
          <SubTitle>{apis[0].date}</SubTitle>
          <PlayIcon
            style={{
              backgroundColor: "white",
              borderRadius: 50,
            }}
          />
        </Wrapper>
      </ImageBackground>
      <StatusBar hidden />
    </>
  }
</>
);
 };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文