通过条件渲染导航到屏幕(如果)

发布于 2025-01-11 20:14:28 字数 3084 浏览 2 评论 0原文

这是入门屏幕,我想在显示或单击最后一个项目时将其导航到屏幕,而不是显示 console.log("last item") 它应该导航到屏幕

 import React,{useState,useRef} from 'react';
    import { View, Text,StyleSheet, FlatList, Animated } from 'react-native';
    
    import OnboardingItem from "./OnboardingItem";
    import slides from "./slides"
    import Paginator from './Paginator';
    import NextButton from './NextButton';
    import WelcomeScreen from "../../screens/WelcomeScreen"
    
    
    export default Onboarding  =  () => {
      const [currentIndex,setCurrentIndex] = useState(0);
    
      const scrollX = useRef(new Animated.Value(0)).current;
    
      const slidesRef = useRef(null);
    
      const viewableItemsChanged = useRef(({viewableItems}) =>{
        setCurrentIndex(viewableItems[0].index );
      }).current;
    
      const viewConfig = useRef({ viewAreaCoveragePercentThreshold:50 }).current;
    
      const scrollTo = () => {
        if(currentIndex < slides.length - 1){
          slidesRef.current.scrollToIndex({index: currentIndex + 1})
        }else{
           console.log("last item");
        }
      }
    
      return (
        <View style={styles.container}>
          <View style={{flex:3}}>
          <FlatList 
          data={slides} 
          renderItem={({item}) => <OnboardingItem item={item} /> } 
          horizontal
          showsHorizontalScrollIndicator={false}
          pagingEnabled
          bounces={false}
          keyExtractor={(item) => item.id }
          onScroll={Animated.event([{nativeEvent: {contentOffset:{x:scrollX} } }],{
            useNativeDriver:false,
          })}
          scrollEventThrottle={32}
          onViewableItemsChanged={viewableItemsChanged}
          viewabilityConfig={viewConfig}
          ref={slidesRef}
          />
          </View>
          <Paginator data={slides} scrollX={scrollX} />
          <NextButton scrollTo={scrollTo} percentage={(currentIndex + 1 ) * (100/slides.length)} />
        </View>
      )
    };
    
    const styles = StyleSheet.create({
        container:{
          flex:1,
          justifyContent:"center",
          alignItems:"center"
        }
    })

,这是我的导航,其中包含

import React from "react";
import { createNativeStackNavigator } from '@react-navigation/native-stack';


import LoginScreen from "../screens/LoginScreen";
import RegisterScreen from "../screens/RegisterScreen";
import WelcomeScreen from "../screens/WelcomeScreen";
import Onboarding from "../components/Getstarted/Onboarding"

const Stack = createNativeStackNavigator();

const AuthNavigator = () => (
  <Stack.Navigator>
    <Stack.Screen
      name="Welcome"
      component={WelcomeScreen}
      options={{ headerShown: false }}
    />
    <Stack.Screen name="Login" component={LoginScreen} />
    <Stack.Screen name="Register" component={RegisterScreen} />
  </Stack.Navigator>
);

export default AuthNavigator;

消费后的 欢迎屏幕很多时候我仍然无法在我的项目中正确导航它,谢谢如果有人可以帮助解决这个问题。

this is the onboarding screen I want to navigate it to a screen when the last item is displayed or clicked instead of showing console.log("last item") it should navigate to a screen

 import React,{useState,useRef} from 'react';
    import { View, Text,StyleSheet, FlatList, Animated } from 'react-native';
    
    import OnboardingItem from "./OnboardingItem";
    import slides from "./slides"
    import Paginator from './Paginator';
    import NextButton from './NextButton';
    import WelcomeScreen from "../../screens/WelcomeScreen"
    
    
    export default Onboarding  =  () => {
      const [currentIndex,setCurrentIndex] = useState(0);
    
      const scrollX = useRef(new Animated.Value(0)).current;
    
      const slidesRef = useRef(null);
    
      const viewableItemsChanged = useRef(({viewableItems}) =>{
        setCurrentIndex(viewableItems[0].index );
      }).current;
    
      const viewConfig = useRef({ viewAreaCoveragePercentThreshold:50 }).current;
    
      const scrollTo = () => {
        if(currentIndex < slides.length - 1){
          slidesRef.current.scrollToIndex({index: currentIndex + 1})
        }else{
           console.log("last item");
        }
      }
    
      return (
        <View style={styles.container}>
          <View style={{flex:3}}>
          <FlatList 
          data={slides} 
          renderItem={({item}) => <OnboardingItem item={item} /> } 
          horizontal
          showsHorizontalScrollIndicator={false}
          pagingEnabled
          bounces={false}
          keyExtractor={(item) => item.id }
          onScroll={Animated.event([{nativeEvent: {contentOffset:{x:scrollX} } }],{
            useNativeDriver:false,
          })}
          scrollEventThrottle={32}
          onViewableItemsChanged={viewableItemsChanged}
          viewabilityConfig={viewConfig}
          ref={slidesRef}
          />
          </View>
          <Paginator data={slides} scrollX={scrollX} />
          <NextButton scrollTo={scrollTo} percentage={(currentIndex + 1 ) * (100/slides.length)} />
        </View>
      )
    };
    
    const styles = StyleSheet.create({
        container:{
          flex:1,
          justifyContent:"center",
          alignItems:"center"
        }
    })

and this is my navigation which consists of welcomescreen

import React from "react";
import { createNativeStackNavigator } from '@react-navigation/native-stack';


import LoginScreen from "../screens/LoginScreen";
import RegisterScreen from "../screens/RegisterScreen";
import WelcomeScreen from "../screens/WelcomeScreen";
import Onboarding from "../components/Getstarted/Onboarding"

const Stack = createNativeStackNavigator();

const AuthNavigator = () => (
  <Stack.Navigator>
    <Stack.Screen
      name="Welcome"
      component={WelcomeScreen}
      options={{ headerShown: false }}
    />
    <Stack.Screen name="Login" component={LoginScreen} />
    <Stack.Screen name="Register" component={RegisterScreen} />
  </Stack.Navigator>
);

export default AuthNavigator;

after spending a lot of time still was am not able to navigate it properly on my project thanks if anyone can help sort this problem out.

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

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

发布评论

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

评论(1

安人多梦 2025-01-18 20:14:28

函数Onboarding可以接受两个参数routenavigation。您可以使用它们导航到其他屏幕

export function Onboarding({
  route,
  navigation
}) {
  // .. rest of the code
  if () {

  } else {
    navigation.navigate('NameOfScree', {
      // options
    })
  }
}

The function Onboarding may be accepting two argument route and navigation. You can use these to navigate to other screen

export function Onboarding({
  route,
  navigation
}) {
  // .. rest of the code
  if () {

  } else {
    navigation.navigate('NameOfScree', {
      // options
    })
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文