강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

khs1003k님의 프로필 이미지
khs1003k

작성한 질문수

React Native with Expo: 제로초에게 제대로 배우기

라우터가 아닌 컴포넌트는 어디에 있어야 할까?

탭 전환 안됨

작성

·

89

·

수정됨

0

강의 잘 따라가다가 로그인까지 성공했는데 20강부터 갑자기 add,activity,user탭을 눌러도 전환이 안돼요.. home이랑 search만 전환이 됩니다. 에러도 안떠서 어디부터 잘못된건지 감이 안 오는데 어떤 파일을 확인해야 할까요?

app/(tabs)_layout.tsx 입니다

import { Ionicons } from "@expo/vector-icons";
import { type BottomTabBarButtonProps } from "@react-navigation/bottom-tabs";
import { Tabs, useRouter } from "expo-router";
import { useContext, useRef, useState } from "react";
import { Animated, Modal, Pressable, Text, TouchableOpacity, View } from "react-native";
import { AuthContext } from "../_layout";


export default function TabLayout(){
  const router=useRouter();
  const [isLoginModalOpen,setIsLoginModalOpen]=useState(false);
  const {user}=useContext(AuthContext);
   const isLoggedIn = !!user;
   

  const openLoginModal=()=>{
    setIsLoginModalOpen(true);
  };
  const closeLoginModal=()=>{
    setIsLoginModalOpen(false);
  };

  const toLoginPage= ()=>{
    setIsLoginModalOpen(false);
    router.push("/login");
  }
  const AnimatedTabBarButton=({
    children,
    onPress,
    style,
    ...restProps
  }: BottomTabBarButtonProps)=>{
    const scaleValue=useRef(new Animated.Value(1)).current;
    const handlePressOut=()=>{
      Animated.sequence([
      Animated.spring(scaleValue,{
        toValue:1.2,
        useNativeDriver:true, 
        speed:200,
      }),
      Animated.spring(scaleValue,{
        toValue:1,
        useNativeDriver:true,
        speed:200,
      }),
    ]).start();
    }
    return (
    <Pressable
      onPress={onPress}
      onPressOut={handlePressOut}
      style={[{ flex: 1, justifyContent: "center", alignItems: "center" }, style]}
      android_ripple={{ borderless: false, radius: 0 }}
    >
      <Animated.View style={{ transform: [{ scale: scaleValue }] }}>
        {children}
      </Animated.View>
    </Pressable>

  );
  }

  return (
    <>
      <Tabs
        backBehavior="history"
        screenOptions={{
          headerShown:false,
          tabBarButton: (props)=><AnimatedTabBarButton {...props}/>,
        }}
       
      >
      <Tabs.Screen 
        name="(home)"
        options={{
          tabBarLabel:()=>null,
          tabBarIcon:({focused})=>(
            <Ionicons
              name="home"
              size={24}
              color={focused ? "black":"gray"}/>
          ),
        }}
        />
      <Tabs.Screen name="search"
       options={{
          tabBarLabel:()=>null,
          tabBarIcon:({focused})=>(
            <Ionicons
              name="search"
              size={24}
              color={focused ? "black":"gray"}/>
          ),
        }}/>
      <Tabs.Screen name="add"
      listeners={{
       tabPress:(e)=>{
        e.preventDefault();
        if(!isLoggedIn){
          openLoginModal();
        }
      }
      }}
      options={{
          tabBarLabel:()=>null,
          tabBarIcon:({focused})=>(
            <Ionicons
              name="add"
              size={24}
              color={focused ? "black":"gray"}/>
          ),
        }}
      />
      <Tabs.Screen name="activity"
      listeners={{
        tabPress:(e)=>{
          e.preventDefault();
          if(!isLoggedIn){
            openLoginModal();
          }
        },
      }}
       options={{
          tabBarLabel:()=>null,
          tabBarIcon:({focused})=>(
            <Ionicons
              name="heart-outline"
              size={24}
              color={focused ? "black":"gray"}/>
          ),
        }}/>
      <Tabs.Screen name="[username]"
      listeners={{
        tabPress:(e)=>{
          e.preventDefault();
          if(!isLoggedIn){
            openLoginModal();
          }
        },
      }}
       options={{
          tabBarLabel:()=>null,
          tabBarIcon:({focused})=>(
            <Ionicons
              name="person-outline"
              size={24}
              color={focused ? "black":"gray"}/>
          ),
        }}/>
        <Tabs.Screen
        name="(post)/[username]/post/[postID]"
        options={{
          href:null,
        }}
      />
      </Tabs>
      <Modal
        visible={isLoginModalOpen} transparent={true} animationType="slide"
        >
        <View
          style={{
            flex:1,
            justifyContent:"flex-end",
            backgroundColor:"rgba(0,0,0,0.5)"
          }}>
            <View style={{backgroundColor:"white",padding:20}}>
              <Pressable onPress={toLoginPage}>
                <Text>Login Modal</Text>
              </Pressable>
              <TouchableOpacity
                onPress={closeLoginModal}>
                <Ionicons name="close" size={24} color='#555'/>
          </TouchableOpacity>
            </View>
        </View>
      </Modal>
      </>
  );
}

답변 2

0

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

아, 지금 눈치챘는데 e.preventDefault();를 if문 안에 넣으셔야 합니다.

0

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

tabPress 내부에 콘솔로그 넣어서 클릭 시 콘솔 뜨나 한 번 해보시고요. 콘솔이 안 뜨거나 하면 앱 지웠다 다시 설치해보셔야 합니다. 에뮬레이터가 이런 문제가 좀 많습니다.

khs1003k님의 프로필 이미지
khs1003k

작성한 질문수

질문하기