undefined is not an object (evaluating 'navigation.navigate') (Device) - reactjs

const Home = ({navigation}) => {
const renderDiscoverItem = ({item}) => {
return (
<TouchableOpacity
onPress={() =>
navigation.navigate('Details', {
item: item,
})
}>
<ImageBackground
source={item.images}
style={[
styles.discoverItem,
{marginLeft: item.id === 'discover-1' ? 20 : 0},
]}
imageStyle={styles.discoverItemImage}>
<Text style={styles.discoverItemTitle}>{item.title}</Text>
<View style={styles.discoverItemLocationWrapper}>
<Entypo name="location-pin" size={18} color={colors.white} />
<Text style={styles.discoverItemLocationText}>{item.location}</Text>
</View>
</ImageBackground>
</TouchableOpacity>
);
};

There were 2 issues,
You were exporting directly Home in App.js which means Home is not wrapped inside any navigator, find below the corrected code for App.js
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Home from './components/Home';
import Details from './components/Details';
import Liked from './components/Liked';
import Profile from './components/Profile';
import colors from './assets/colors/colors';
import Entypo from 'react-native-vector-icons/Entypo';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import {NavigationContainer} from '#react-navigation/native';
import {createStackNavigator} from '#react-navigation/stack';
import {createBottomTabNavigator} from '#react-navigation/bottom-tabs';
Entypo.loadFont();
MaterialCommunityIcons.loadFont();
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();
const TabNavigator = () => {
return (
<Tab.Navigator
tabBarOptions={{
style: styles.tabBar,
activeTintColor: colors.orange,
inactiveTintColor: colors.gray,
showLable: false,
}}>
<Tab.Screen
name="Home"
component={Home}
options={{
tabBarIcon: ({color}) => (
<Entypo name="home" size={32} color={color} />
),
}}
/>
<Tab.Screen
name="Liked"
component={Liked}
options={{
tabBarIcon: ({color}) => (
<Entypo name="heart" size={32} color={color} />
),
}}
/>
<Tab.Screen
name="Profile"
component={Profile}
options={{
tabBarIcon: ({color}) => (
<MaterialCommunityIcons name="account" size={32} color={color} />
),
}}
/>
</Tab.Navigator>
);
};
const App = () => {
console.log('inside app')
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="TabNavigator"
component={TabNavigator}
options={{headerShown: false}}
/>
<Stack.Screen
name="Details"
component={Details}
options={{headerShown: false}}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
const styles = StyleSheet.create({
tabBar: {
backgroundColor: colors.white,
borderTapLeftRedius: 20,
borderTopRightRadius: 20,
},
});
export default App;
Second mistake is the usage of useNavigation hook, it must be used as below in the corrected code,
import React from 'react';
import {
View,
Text,
StyleSheet,
Image,
ScrollView,
ImageBackground,
AppRegistry,
} from 'react-native';
import colors from '../assets/colors/colors';
import Feather from 'react-native-vector-icons/Feather';
import Entypo from 'react-native-vector-icons/Entypo';
import activitiesData from '../assets/data/activitiesData';
import discoverCategoriesData from '../assets/data/discoverCategoriesData';
import learnMoreData from '../assets/data/learnMoreData';
import discoverData from '../assets/data/discoverData';
import {SafeAreaView} from 'react-native-safe-area-context';
import profile from '../assets/images/profile.png';
import {FlatList, TouchableOpacity} from 'react-native-gesture-handler';
import { useNavigation } from '#react-navigation/native';
Feather.loadFont();
Entypo.loadFont();
const Home = () => {
const navigation = useNavigation();
const renderDiscoverItem = ({item}) => {
return (
<TouchableOpacity
onPress={() =>
navigation.navigate('Details', {
item: item,
})
}>
<ImageBackground
source={item.images}
style={[
styles.discoverItem,
{marginLeft: item.id === 'discover-1' ? 20 : 0},
]}
imageStyle={styles.discoverItemImage}>
<Text style={styles.discoverItemTitle}>{item.title}</Text>
<View style={styles.discoverItemLocationWrapper}>
<Entypo name="location-pin" size={18} color={colors.white} />
<Text style={styles.discoverItemLocationText}>{item.location}</Text>
</View>
</ImageBackground>
</TouchableOpacity>
);
};
const renderActivityItem = ({item}) => {
return (
<View
style={[
styles.activityItemWrapper,
{
marginLeft: item.id === 'activities-1' ? 20 : 0,
},
]}>
<Image source={item.image} style={styles.activityItemImage} />
<Text style={styles.activityItemText}>{item.title}</Text>
</View>
);
};
const renderLearnMoreItem = ({item}) => {
return (
<ImageBackground
source={item.images}
style={[
styles.learnMoreItem,
{
marginLeft: item.id === 'learnMore-1' ? 20 : 0,
},
]}
imageStyle={styles.learnMoreItemImage}>
<Text style={styles.learnMoreItemText}>{item.title}</Text>
</ImageBackground>
);
};
return (
<View style={styles.container}>
<ScrollView>
{/* Header */}
<SafeAreaView>
<View style={styles.menuWrapper}>
<Feather
name="menu"
size={32}
color={colors.black}
style={styles.menuIcon}
/>
<Image source={profile} style={styles.profileImage} />
</View>
</SafeAreaView>
{/* Discover */}
<View style={styles.discoverWrapper}>
<Text style={styles.discoverTitle}>Discover</Text>
<View style={styles.discoverCategoriesWrapper}>
<Text style={[styles.discoverCategoryText, {color: colors.orange}]}>
All
</Text>
<Text style={styles.discoverCategoryText}>Destinations</Text>
<Text style={styles.discoverCategoryText}>Cities</Text>
<Text style={styles.discoverCategoryText}>Experiences</Text>
</View>
<View style={styles.discoverItemsWrapper}>
<FlatList
data={discoverData}
renderItem={renderDiscoverItem}
keyExtractor={(item) => item.id}
horizontal
showsHorizontalScrollIndicator={false}
/>
</View>
</View>
{/* Activities */}
<View style={styles.activitiesWrapper}>
<Text style={styles.activitiesTitle}>Activities</Text>
<View style={styles.activitiesItemsWrapper}>
<FlatList
data={activitiesData}
renderItem={renderActivityItem}
keyExtractor={(item) => item.id}
horizontal
showsHorizontalScrollIndicator={false}
/>
</View>
</View>
{/* Learn More */}
<View style={styles.learnMoreWrapper}>
<Text style={styles.learnMoreTitle}>Learn More</Text>
<View style={styles.learnMoreItemsWrapper}>
<FlatList
data={learnMoreData}
renderItem={renderLearnMoreItem}
keyExtractor={(item) => item.id}
horizontal
showsHorizontalScrollIndicator={false}
/>
</View>
</View>
</ScrollView>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
color: colors.white,
},
menuWrapper: {
marginHorizontal: 20,
marginTop: 20,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
profileImage: {
width: 52,
height: 52,
borderRadius: 10,
},
discoverWrapper: {
// marginHorizontal: 20,
marginTop: 20,
},
discoverTitle: {
marginHorizontal: 20,
fontFamily: 'Lato-Bold',
fontSize: 32,
},
discoverCategoriesWrapper: {
marginHorizontal: 20,
flexDirection: 'row',
marginTop: 20,
},
discoverCategoryText: {
marginRight: 30,
fontFamily: 'Lato-Regular',
fontSize: 16,
color: colors.gray,
},
discoverItemsWrapper: {
paddingVertical: 20,
},
discoverItem: {
width: 170,
height: 250,
justifyContent: 'flex-end',
paddingHorizontal: 10,
paddingVertical: 15,
marginRight: 20,
},
discoverItemImage: {
borderRadius: 20,
},
discoverItemTitle: {
fontFamily: 'Lato-Bold',
fontSize: 18,
color: colors.white,
},
discoverItemLocationWrapper: {
flexDirection: 'row',
alignItems: 'center',
marginTop: 5,
},
discoverItemLocationText: {
marginLeft: 5,
fontFamily: 'Lato-Bold',
fontSize: 14,
color: colors.white,
},
activitiesWrapper: {
marginTop: 10,
},
activitiesTitle: {
marginHorizontal: 20,
fontFamily: 'Lato-Bold',
fontSize: 24,
color: colors.black,
},
activitiesItemsWrapper: {
paddingVertical: 20,
},
activityItemWrapper: {
justifyContent: 'flex-end',
alignItems: 'center',
marginRight: 20,
},
activityItemImage: {
width: 36,
},
activityItemText: {
marginTop: 5,
fontFamily: 'Lato-Bold',
fontSize: 14,
color: colors.gray,
},
learnMoreWrapper: {
marginTop: 10,
},
learnMoreTitle: {
marginHorizontal: 20,
fontFamily: 'Lato-Bold',
fontSize: 24,
color: colors.black,
},
learnMoreItemsWrapper: {
paddingVertical: 20,
},
learnMoreItem: {
width: 170,
height: 180,
justifyContent: 'flex-end',
marginRight: 20,
},
learnMoreItemImage: {
borderRadius: 20,
},
learnMoreItemText: {
fontFamily: 'Lato-Bold',
fontSize: 18,
color: colors.white,
marginHorizontal: 10,
marginVertical: 20,
},
});
export default Home;
Fully working code can be found here

Related

Why is my scrollview not showing last element normally?

Lessonlist in the app component displays a list of lessons, if there are a lot of them, scrolling works but does not show all the elements. That is, some element remains at the bottom and is only half visible. When the height of the lessonlist changes, the height of the dataslider component also changes.
App
return (
<View style={styles.container}>
<StatusBar style="auto" />
<View style={styles.today}>
<Moment element={Text} style={styles.today_day} format='D'></Moment>
<View style={styles.today_column}>
<Moment element={Text} style={styles.today_day_week} format='dddd'></Moment>
<Moment element={Text} style={styles.today_month_year} format='MMMM YYYY'></Moment>
</View>
</View>
<View>
<DateSlider data={Lessons} index={index} setIndex={setIndex} />
<LessonList data={Lessons} index={index} setIndex={setIndex} />
</View>
</View>
);
LessonList
import React from 'react';
import { StyleSheet, Text, View, FlatList, Dimensions, ScrollView } from 'react-native'
import AntDesign from '#expo/vector-icons/AntDesign';
const { width, height } = Dimensions.get('screen');
const LessonList = ({ data, index, setIndex }) => {
const lessonsRef = React.useRef<FlatList>();
React.useEffect(() => {
lessonsRef.current?.scrollToOffset({
offset: index * width,
animated: true,
});
}, [index]);
return (
<FlatList
ref={lessonsRef}
initialNumToRender={1}
initialScrollIndex={index}
data={data.days}
maxToRenderPerBatch={3}
keyExtractor={(item) => item.date}
getItemLayout={(data, index) => ({
length: width,
offset: width * index,
index,
})}
onMomentumScrollEnd={(ev) => {
setIndex(Math.floor(Math.floor(ev.nativeEvent.contentOffset.x) / Math.floor(width)));
}}
horizontal
pagingEnabled
showsHorizontalScrollIndicator={false}
renderItem={({ item }) => {
return (
<FlatList
data={item.lessons}
keyExtractor={(item) => item.id}
showsVerticalScrollIndicator={false}
scrollEnabled={true}
style={styles.lessons_scrollview}
renderItem={({ item }) => {
return (
<View style={styles.lessons}>
<View style={styles.lesson_time}>
<Text style={styles.lesson_time_list_text}>
{item.starttime}
</Text>
<Text style={styles.lesson_time_end_list_text}>
{item.endtime}
</Text>
</View>
<View style={styles.lesson_card}>
<Text style={styles.lesson_card_name}>{item.name}</Text>
<Text style={styles.lesson_card_description}>
{item.description}
</Text>
<Text style={styles.lesson_card_locate}>
<AntDesign name="enviromento" size={16} color="white" />
{item.location}
</Text>
<Text style={styles.lesson_card_teacher}>
<AntDesign name="user" size={16} color="white" />
{item.teacher}
</Text>
</View>
</View>
);
}}
/>
);
}}
/>
);
};
const styles = StyleSheet.create({
lessons_scrollview: {
paddingHorizontal: 15,
paddingTop: 5,
width: width,
},
lessons: {
flexDirection: "row",
},
lesson_time_text: {
fontFamily: "eUkraineBold",
fontSize: 9,
paddingRight: 30,
color: "#BCC1CD",
},
lesson_time: {
flexDirection: "column",
paddingRight: 9,
borderRightWidth: 1,
borderRightColor: "#FAF9F9",
},
lessons_text: {
fontFamily: "eUkraineBold",
fontSize: 9,
color: "#BCC1CD",
},
lesson_time_list: {
flexDirection: "column",
paddingTop: 14,
},
lesson_time_list_text: {
fontFamily: "eUkraineMedium",
fontSize: 14,
},
lesson_time_end_list_text: {
fontFamily: "eUkraineMedium",
fontSize: 14,
color: "#BCC1CD",
},
lesson_card: {
flexDirection: "column",
marginLeft: 16,
backgroundColor: "#4DC591",
borderRadius: 16,
paddingTop: 16,
paddingLeft: 16,
paddingBottom: 17,
flex: 1,
marginBottom: 16,
},
lesson_card_name: {
fontFamily: "eUkraineBold",
fontSize: 13,
color: "#ffff",
},
lesson_card_description: {
fontFamily: "eUkraineMedium",
fontSize: 10,
paddingTop: 4,
color: "#ffff",
},
lesson_card_locate_img: {
height: 16,
width: 16,
marginRight: 50,
tintColor: "#FFFFFF",
},
lesson_card_locate: {
fontFamily: "eUkraineRegular",
fontSize: 10,
paddingTop: 15,
color: "#ffff",
},
lesson_card_teacher: {
fontFamily: "eUkraineRegular",
fontSize: 10,
paddingTop: 3,
color: "#ffff",
},
});
export default LessonList

CustomDrawerNavigator: Change Active and Inactive Background Colour

On react-navigation/drawer's DrawerItem, is there a way to add active and inactive background colour? I followed this document to implement this https://reactnavigation.org/docs/drawer-navigator/.
I have added theses code lines to the drawerItems. But it won't work for me.
drawerContentOptions={{
activeTintColor: '#fff', /* font color for active screen label */
activeBackgroundColor: '#68f', /* bg color for active screen */
inactiveTintColor: 'grey', /* Font color for inactive screens' labels */
}}
initialized by the following,
import * as React from 'react';
import {
Button,
View,
Text,
StyleSheet,
TouchableOpacity,
Image,
} from 'react-native';
import { useDispatch, useSelector } from 'react-redux';
import {
createDrawerNavigator,
DrawerContentScrollView,
DrawerItem,
} from '#react-navigation/drawer';
import Home from '../home/containers';
import NavigationService from '../../navigation/NavigationService';
import * as homeActions from '../../features/home/actions';
import * as loginActions from '../../features/login/actions';
import { Images } from '../../config';
import i18n from 'i18n-js';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
onPress={() => NavigationService.navigate('Notifications')}
title="Go to notifications"
/>
</View>
);
}
function NotificationsScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => NavigationService.goBack()} title="Go back home" />
</View>
);
}
function CustomDrawerContent({ props, navigation }) {
const dispatch = useDispatch();
const outlets = useSelector((state) => state.homeReducer.outlets);
const defaultOutLet = useSelector((state) => state.homeReducer.defaultOutLet);
const drawerItems = outlets.map((outlet) => {
return (
console.log("############## nav drawr ################## " + outlet.name),
<DrawerItem
{...props}
key={outlet.userCompanyId}
label={outlet.name}
onPress={() => {
dispatch(homeActions.changeSelectedOutlet(outlet));
navigation.closeDrawer();
}}
/>
);
});
return (
<View style={styles.flexView}>
<View style={styles.container}>
<Image
style={styles.image}
source={Images.icons.logo}
resizeMode="contain"
/>
{defaultOutLet && <Text style={styles.text}>{defaultOutLet.name}</Text>}
</View>
<View style={styles.separator} />
<View style={styles.flexView}>{drawerItems}</View>
<View style={styles.separator} />
<View style={styles.bottomView}>
<TouchableOpacity
style={styles.logoutContainer}
onPress={() => {
dispatch(homeActions.clearUserCompany());
dispatch(loginActions.completeLogOutClearingUserData());
}}>
<Image
style={styles.logoutIcon}
source={Images.icons.power}
resizeMode="center"
/>
<Text style={styles.logoutText}>{i18n.t('common.logout')}</Text>
</TouchableOpacity>
</View>
</View>
);
}
const Drawer = createDrawerNavigator();
export default function DrawerNavigator() {
return (
<Drawer.Navigator
drawerContent={(props) => <CustomDrawerContent {...props} />}
initialRouteName="Home">
<Drawer.Screen name="Home" component={Home} />
</Drawer.Navigator>
);
}
const styles = StyleSheet.create({
container: {
flex: 0.3,
width: '100%',
backgroundColor: 'white',
},
flexView: {
flex: 1,
},
text: {
marginVertical: 12,
color: 'black',
textAlign: 'center',
},
logoutContainer: {
flexDirection: 'row',
alignItems: 'center',
width: '100%',
},
logoutText: {
color: 'gray',
},
logoutIcon: {
flex: 0.4,
},
image: {
flex: 1,
marginTop: 20,
alignSelf: 'center',
},
separator: {
width: '100%',
height: 1,
backgroundColor: 'gray',
},
bottomView: {
flex: 0.15,
width: '100%',
justifyContent: 'center',
},
});

Add submenu on drawer react native

I am using below code to show a menu on react native. I need to show a submenu but I cannot figure out how.
<Drawer.Navigator
drawerContentOptions={{
activeTintColor: '#cee1f2',
color: '#cee1f2',
itemStyle: {marginVertical: 5, color: 'white'},
labelStyle: {
color: '#d8d8d8',
},
}}
screenOptions={{headerShown: false}}
drawerContent={CustomSidebarMenu}>
<Drawer.Screen
name="homeScreenStack"
options={{drawerLabel: 'Home Screen'}}
component={homeScreenStack}
/>
<Drawer.Screen
name="settingScreenStack"
options={{drawerLabel: 'Setting Screen'}}
component={settingScreenStack}
/>
</Drawer.Navigator>
Here is Customesidebar code.
// Import React and Component
import React from 'react';
import {View, Text, Alert, StyleSheet} from 'react-native';
import {
DrawerContentScrollView,
DrawerItemList,
DrawerItem,
} from '#react-navigation/drawer';
import AsyncStorage from '#react-native-community/async-storage';
const CustomSidebarMenu = (props) => {
return (
<View style={stylesSidebar.sideMenuContainer}>
<View style={stylesSidebar.profileHeader}>
<View style={stylesSidebar.profileHeaderPicCircle}>
<Text style={{fontSize: 25, color: '#307ecc'}}>
{'DishUp'.charAt(0)}
</Text>
</View>
<Text style={stylesSidebar.profileHeaderText}>
DishUp
</Text>
</View>
<View style={stylesSidebar.profileHeaderLine} />
<DrawerContentScrollView {...props}>
<DrawerItemList {...props} />
<DrawerItem
label={({color}) =>
<Text style={{color: '#d8d8d8'}}>
Logout
</Text>
}
onPress={() => {
props.navigation.toggleDrawer();
Alert.alert(
'Logout',
'Are you sure? You want to logout?',
[
{
text: 'Cancel',
onPress: () => {
return null;
},
},
{
text: 'Confirm',
onPress: () => {
AsyncStorage.clear();
props.navigation.replace('Auth');
},
},
],
{cancelable: false},
);
}}
/>
</DrawerContentScrollView>
</View>
);
};
export default CustomSidebarMenu;
const stylesSidebar = StyleSheet.create({
sideMenuContainer: {
width: '100%',
height: '100%',
backgroundColor: '#307ecc',
paddingTop: 40,
color: 'white',
},
profileHeader: {
flexDirection: 'row',
backgroundColor: '#307ecc',
padding: 15,
textAlign: 'center',
},
profileHeaderPicCircle: {
width: 60,
height: 60,
borderRadius: 60 / 2,
color: 'white',
backgroundColor: '#ffffff',
textAlign: 'center',
justifyContent: 'center',
alignItems: 'center',
},
profileHeaderText: {
color: 'white',
alignSelf: 'center',
paddingHorizontal: 10,
fontWeight: 'bold',
},
profileHeaderLine: {
height: 1,
marginHorizontal: 20,
backgroundColor: '#e2e2e2',
marginTop: 15,
},
});
Here is what it looks like right now. But I want to add a submenu inside Home and Screen. How can I achieve this?

Getting a navigation.navigate type error after spliting the mainscreen into components

I was working on my app with two screens a Main Screen and a Details Screen everything was working, but the code was long, so I tried to split everything into reusable components which caused a
navigation.navigates type error for some reason.
I checked the code multiple times everything makes perfect sense to me is there something am missing here, how do I fixed this error?
Am using react navigation version 5 for the first time.
Here is a sample of the code used:
MainScreen.js
import React from "react";
import { StyleSheet, Text, View, Image, FlatList } from "react-native";
import ArticleList from "../components/ArticleList";
function MainScreen() {
return (
<View style={{ flex: 1 }}>
{/* show the data in a flatlist */}
<ArticleList />
</View>
);
}
MainScreen.navigationOptions = () => {
return {
headerShown: false,
};
};
export default MainScreen;
DetailScreen.js
import React from "react";
import { StyleSheet, Text, View, Dimensions, Image } from "react-native";
import { Feather } from "#expo/vector-icons";
import { SharedElement } from "react-native-shared-element";
import { TouchableOpacity, ScrollView } from "react-native-gesture-handler";
const DetailScreen = (props) => {
const { width, height } = Dimensions.get("window");
const { data } = props.route.params;
return (
<View style={styles.container}>
<View>
<SharedElement id={`item.${data.id}.photo`}>
<Image
resizeMode="cover"
source={{ uri: data.image }}
style={{
width: 400,
height: 300,
borderBottomLeftRadius: 10,
borderBottomRightRadius: 10,
}}
/>
</SharedElement>
<View
style={{
flexDirection: "row",
alignItems: "center",
position: "absolute",
bottom: 14,
left: 10,
}}
>
<SharedElement id={`item.${data.id}.profilePic`}>
<Image
resizeMode="cover"
source={{ uri: data.profilePic }}
style={{
width: 60,
height: 60,
borderRadius: 10,
marginRight: 14,
}}
/>
</SharedElement>
<View
style={{
flex: 1,
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
}}
>
<View>
<SharedElement id={`item.${data.id}.username`}>
<Text
style={{ color: "white", fontSize: 16, fontWeight: "bold" }}
>
{data.username}
</Text>
</SharedElement>
<SharedElement id={`item.${data.id}.readtime`}>
<Text style={{ color: "white", fontSize: 14 }}>
{data.readtime}
</Text>
</SharedElement>
</View>
<TouchableOpacity>
<Feather name="bookmark" size={30} color="white" />
</TouchableOpacity>
</View>
</View>
</View>
<ScrollView style={{ paddingHorizontal: 10, paddingTop: 14 }}>
<SharedElement
id={`item.${data.id}.text`}
style={{ width: width - 30, marginBottom: 14 }}
>
<Text style={{ fontSize: 22, fontWeight: "bold", lineHeight: 32 }}>
{data.title}
</Text>
</SharedElement>
<Text
style={{
fontSize: 14,
lineHeight: 28,
textAlign: "justify",
opacity: 0.5,
}}
>
Paragraph 1
</Text>
<Text
style={{
fontSize: 14,
lineHeight: 28,
textAlign: "justify",
opacity: 0.5,
}}
>
Paragraph 2
</Text>
<View
style={{
marginVertical: 25,
paddingBottom: 20,
flex: 1,
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
}}
>
<TouchableOpacity
style={{ flexDirection: "row", padding: 12, alignItems: "center" }}
>
<Feather name="heart" size={16} color="orange" />
<Text style={{ marginHorizontal: 10 }}>3.4k Likes</Text>
</TouchableOpacity>
</View>
</ScrollView>
<View style={{ position: "absolute", top: 40, left: 10 }}>
<TouchableOpacity onPress={() => props.navigation.goBack()}>
<Feather name="arrow-left" size={24} color="white" />
</TouchableOpacity>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
export default DetailScreen;
ArticleList.js
import React from "react";
import {
View,
Text,
StyleSheet,
Image,
Dimensions,
FlatList,
} from "react-native";
import { SharedElement } from "react-native-shared-element";
import TouchableScale from "react-native-touchable-scale";
import { data } from "../data";
function ArticleList({ navigation }) {
const { width, height } = Dimensions.get("window");
return (
<View>
<FlatList
horizontal
showsHorizontalScrollIndicator={false}
style={{ paddingHorizontal: 30 }}
data={data}
keyExtractor={(item) => item.id}
renderItem={({ item }) => {
return (
<View>
<View>
<TouchableScale
activeScale={0.9}
tension={50}
friction={7}
useNativeDriver
onPress={() =>
navigation.navigate("DetailScreen", { data: item })
}
>
{/* to show the horizental news list*/}
<SharedElement id={`item.${item.id}.photo`}>
<Image
source={{ uri: item.image }}
style={{
width: width - 100,
height: height - 350,
borderRadius: 14,
marginRight: 30,
}}
/>
</SharedElement>
{/* to show the news titles inside the pictures*/}
<SharedElement
id={`item.${item.id}.text`}
style={{
width: width - 100,
position: "absolute",
bottom: 90,
left: 10,
paddingHorizontal: 10,
}}
>
<Text style={styles.blogTitle}>{item.title}</Text>
</SharedElement>
{/* to show the pictre of the author of the news article*/}
<View
style={{
flexDirection: "row",
alignItems: "center",
position: "absolute",
bottom: 20,
left: 20,
}}
>
<SharedElement id={`item.${item.id}.profilePic`}>
<Image
resizeMode="cover"
source={{ uri: item.profilePic }}
style={styles.blogProfilePic}
/>
</SharedElement>
</View>
{/* to show the name of the author and read time of article*/}
<View>
<SharedElement id={`item.${item.id}.username`}>
<Text style={styles.blogUsername}>{item.username}</Text>
</SharedElement>
<SharedElement id={`item.${item.id}.readtime`}>
<Text style={styles.readtime}>{item.readtime}</Text>
</SharedElement>
</View>
</TouchableScale>
</View>
</View>
);
}}
/>
</View>
);
}
const styles = StyleSheet.create({
blogTitle: {
color: "white",
fontSize: 24,
fontWeight: "bold",
lineHeight: 28,
},
blogProfilePic: {
height: 50,
width: 50,
borderRadius: 10,
marginRight: 14,
},
blogUsername: {
color: "white",
fontSize: 16,
fontWeight: "bold",
},
readtime: {
fontSize: 14,
color: "white",
},
});
export default ArticleList;
App.js
import React from "react";
import "react-native-gesture-handler";
import { createSharedElementStackNavigator } from "react-navigation-shared-element";
import { NavigationContainer } from "#react-navigation/native";
import MainScreen from "./app/screens/MainScreen";
import DetailScreen from "./app/screens/DetailScreen";
const Stack = createSharedElementStackNavigator();
const App = ({ navigation }) => {
return (
<NavigationContainer>
<Stack.Navigator
initialRouteName="MainScreen"
screenOptions={{ headerShown: false }}
>
<Stack.Screen name="MainScreen" component={MainScreen} />
<Stack.Screen
name="DetailScreen"
component={DetailScreen}
options={(navigation) => ({
headerBackTitleVisible: false,
cardStyleInterpolator: ({ current: { progress } }) => {
return {
cardStyle: {
opacity: progress,
},
};
},
})}
sharedElements={(route) => {
const { data } = route.params;
return [
{
id: `item.${data.id}.photo`,
animation: "move",
resize: "clip",
align: "center-top",
},
{
id: `item.${data.id}.text`,
animation: "fade",
resize: "clip",
align: "left-center",
},
{
id: `item.${data.id}.profilePic`,
animation: "move",
resize: "clip",
align: "left-center",
},
{
id: `item.${data.id}.username`,
animation: "fade",
resize: "clip",
align: "left-center",
},
{
id: `item.${data.id}.readtime`,
animation: "fade",
resize: "clip",
align: "left-center",
},
];
}}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
My apologies for the long samples of code I tried to keep everything that is connected directly to the problem.
The problem is the you are accessing navigation outside the navigation stack. When you moved the flatlist to to ArticleList its outside the navigation and it wont get the navigation prop.
You can handle this in two ways.
You can simply pass the navigation from the main screen
function MainScreen({ navigation }) {
return (
{/* show the data in a flatlist */}
< ArticleList navigation={navigation} />
);
}
You can use the useNavigation hook to access navigation outside navigation.

Why is extended stylesheet not rendering?

Brand new project. react-native-extended-stylesheet installed. everything working fine, and then out of nowhere it stops working. here's the full component:
import React from 'react';
import { Image, FlatList, View, Text, StyleSheet, SafeAreaView, TouchableOpacity, Dimensions } from 'react-native';
import EStyleSheet from 'react-native-extended-stylesheet';
const { height, width } = Dimensions.get('window');
const data = [
{
title: 'Johnny Appleseed',
subtitle: 'November 12, 2019',
img: require('./src/assets/img/johnny_appleseed.png'),
type: 'video'
},
{
title: 'Johnathan Doe',
subtitle: 'November 19, 2019',
img: require('./src/assets/img/johnny_appleseed.png'),
type: 'voice'
}
];
const AgendaItem = (props) => {
return (
<TouchableOpacity onPress={props.onPress}>
<View style={styles.container}>
<View style={styles.leftContainer}>
<Image
resizeMethod={'scale'}
source={
props.type === 'voice' ? (
require('./src/assets/img/voice_icon.png')
) : (
require('./src/assets/img/video_icon.png')
)
}
style={styles.icon}
/>
<View style={styles.textContainer}>
<Text style={styles.title}>{props.title}</Text>
<Text style={styles.subtitle}>{props.subtitle}</Text>
</View>
</View>
<Image source={props.img} style={styles.img} />
</View>
</TouchableOpacity>
);
};
const ProfileList = () => {
return (
<SafeAreaView>
<FlatList
data={data}
renderItem={({ item }) => (
<AgendaItem title={item.title} subtitle={item.subtitle} img={item.img} type={item.type} />
)}
keyExtractor={(item) => item.id}
/>
</SafeAreaView>
);
};
const styles = EStyleSheet.create({
$rem: width > 380 ? 18 : 16,
container: {
flexDirection: 'row',
backgroundColor: '#fff',
borderRadius: 25,
elevation: 3,
marginBottom: 5,
width: '22rem',
justifyContent: 'space-between',
marginHorizontal: '0.5rem',
paddingVertical: '.75rem',
paddingHorizontal: '1.5rem'
},
textContainer: {
justifyContent: 'space-around'
},
leftContainer: {
flexDirection: 'row',
alignItems: 'center'
},
title: {
fontWeight: 'bold',
fontSize: 20,
color: '#454A66'
},
subtitle: {
color: '#454A66',
fontSize: 12
},
img: {
height: '3rem',
width: '3rem',
borderWidth: 1,
borderColor: '#169C75',
borderRadius: 30
},
icon: {
height: '1.5rem',
width: '1.5rem'
}
});
export default ProfileList;
I was just messing with some of the properties and all of a sudden it stopped rendering any style in the styles object. ive tried closing/opening the project. ive tried rebooting system. this just seems buggy because its happened before.
any suggestions or something I missed?

Resources