작성
·
465
1
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import React from 'react';
import Home from './src/screens/Home';
import Search from './src/screens/Search';
import Profile from './src/screens/Profile';
import Activity from './src/screens/Activity';
import {NavigationContainer} from '@react-navigation/native';
import Status from './src/screens/Status';
import FriendProfile from './src/screens/FriendProfile';
import EditProfile from './src/screens/EditProfile';
const App = () => {
const Stack = createNativeStackNavigator();
const Tab = createBottomTabNavigator();
const BottomTabScreen = () => {
return (
<Tab.Navigator
screenOptions={() => ({
tabBarHideOnKeyboard: true,
// tabBarShowLabel: false,
headerShown: false,
tabBarStyle: {
height: 70,
},
})}>
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Search" component={Search} />
<Tab.Screen name="Activity" component={Activity} />
<Tab.Screen name="Profile" component={Profile} />
</Tab.Navigator>
);
};
return (
<NavigationContainer>
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name="Bottom" component={BottomTabScreen} />
<Stack.Screen name="Status" component={Status} />
<Stack.Screen name="FriendProfile" component={FriendProfile} />
<Stack.Screen name="EditProfile" component={EditProfile} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
강의 내용과 같이 App 컴포넌트 안에서
BottomTabScreen 컴포넌트를 선언하면
"Do not define components during render." 라는
경고문이 뜹니다.
그래서 아래와 같이 코드를 수정하였는데
App 컴포넌트 바깥에서 이렇게 선언해도 문제가 없는건가요?
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import React from 'react';
import Home from './src/screens/Home';
import Search from './src/screens/Search';
import Profile from './src/screens/Profile';
import Activity from './src/screens/Activity';
import {NavigationContainer} from '@react-navigation/native';
import Status from './src/screens/Status';
import FriendProfile from './src/screens/FriendProfile';
import EditProfile from './src/screens/EditProfile';
const Stack = createNativeStackNavigator();
const Tab = createBottomTabNavigator();
const BottomTabScreen = () => {
return (
<Tab.Navigator
screenOptions={() => ({
tabBarHideOnKeyboard: true,
// tabBarShowLabel: false,
headerShown: false,
tabBarStyle: {
height: 70,
},
})}>
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Search" component={Search} />
<Tab.Screen name="Activity" component={Activity} />
<Tab.Screen name="Profile" component={Profile} />
</Tab.Navigator>
);
};
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name="Bottom" component={BottomTabScreen} />
<Stack.Screen name="Status" component={Status} />
<Stack.Screen name="FriendProfile" component={FriendProfile} />
<Stack.Screen name="EditProfile" component={EditProfile} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
답변 3
0
추가로, 아이콘 이름 넣는 부분에서
24년5월7일 기준으로 아이콘 이름이 변경된 것 같네요.
"react-native-vector-icons": "^10.1.0")
if (route.name === 'Home') {
iconName = focused ? 'home-sharp' : 'home-outline';
} else if (route.name === 'Search') {
iconName = focused ? 'search' : 'search-outline';
} else if (route.name === 'Activity') {
iconName = focused ? 'heart' : 'heart-outline';
} else if (route.name === 'Profile') {
iconName = focused ? 'person-circle' : 'person-outline';
}
0
0
수정한 코드로도 기능 자체는 정상적으로 동작을 하는데, 이런 방식으로 컴포넌트 바깥에서 변수나 함수를 선언하면 좋지 않다는 글을 어딘가에서 본 기억이 나서 질문 드렸습니다!