I am trying to reuse my home screen with different query, following is my code. But the route.params
is undefined.
//screen I want to reuse
const Home = () => {
const [products, setProducts] = useState([]);
const route = useRoute();
const navigation = useNavigation();
console.log(route);
useEffect(() => {
if (route.params?.data) {
console.log(route.params?.data);
// Post updated, do something with `route.params.post`
// For example, send the post to the server
}
const fetchProducts = async () => {
const results = await DataStore.query(Product);
setProducts(results);
};
fetchProducts();
}, [route.params?.data]);
return (
<View>
<FlatList
data={products}
renderItem={({item}) => <Post post={item} />}
showsVerticalScrollIndicator={false}
snapToInterval={Dimensions.get('window').height - 70}
snapToAlignment={'start'}
decelerationRate={'fast'}
removeClippedSubviews={true}
/>
</View>
);
};
const ProfileScreen = () => {
const [user, setUser] = useState([]);
const [product, setProduct] = useState([]);
const route = useRoute();
const navigation = useNavigation();
const userid = route.params.data;
//const picture = await DataStore.query(Post, "123");
useEffect(() => {
DataStore.query(User, userid).then(setUser);
const fetchProducts = async () => {
const userProduct = (await DataStore.query(Product)).filter(
c => c.user.id === userid,
);
//console.log('products', userProduct);
setProduct(userProduct);
};
fetchProducts();
}, []);
const thumbPress = post => {
navigation.navigate('HomeScreen', {data: post});
console.log(post);
};
//console.log(product);
return (
<ScrollView>
<View style={styles.container}>
<View style={styles.profile}>
<Image source={{uri: user.imageUri}} style={styles.profilePicture} />
<Text>{user.username}</Text>
</View>
{product.map((post, index) => (
<View style={[styles.box]} key={index}>
<TouchableOpacity onPress={thumbPress.bind(this, post)}>
<Image source={{uri: post.images[0]}} style={styles.thumbnail} />
</TouchableOpacity>
</View>
))}
</View>
</ScrollView>
);
};
export default ProfileScreen;
const Stack = createStackNavigator();
const RootNavigation = () => {
return (
<NavigationContainer>
<Stack.Navigator
initialRouteName="Home"
screenOptions={{
headerShown: false,
}}>
<Stack.Screen name="Home" component={HomeBottomTabNavigator} />
<Stack.Screen
options={{
headerShown: false,
title: 'AuthScreen',
}}
name="AuthScreen"
component={AuthScreen}
/>
<Stack.Screen
options={{
headerShown: true,
title: 'Post',
}}
name="CreatePost"
component={CreatePost}
/>
<Stack.Screen
options={{
headerShown: true,
title: 'Profile',
}}
name="ProfileScreen"
component={ProfileScreen}
/>
<Stack.Screen
options={{
headerShown: false,
title: 'Camera',
}}
name="Camera"
component={Camera}
/>
<Stack.Screen
options={{
headerShown: true,
title: 'Upload Options',
}}
name="UploadScreen"
component={UploadScreen}
/>
<Stack.Screen
options={{
headerShown: false,
title: 'ProductScreen',
}}
name="ProductScreen"
component={ProductScreen}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
export default RootNavigation;
const Tab = createBottomTabNavigator();
const HomeBottomTabNavigator = () => {
return (
<Tab.Navigator
tabBarOptions={{
tabStyle: {
borderStartColor: 'red',
},
activeTintColor: 'white',
}}>
<Tab.Screen
name={'Home'}
component={Home}
options={{
tabBarIcon: ({color}) => <Entypo name={'home'} size={24} />,
}}
/>
<Tab.Screen
name={'Upload'}
component={UploadScreen}
options={{
tabBarIcon: ({color}) => (
<Ionicons name={'person-outline'} size={24} />
),
}}
/>
<Tab.Screen
name={'Search'}
component={Home}
options={{
tabBarIcon: ({color}) => <AntDesign name={'search1'} size={24} />,
}}
/>
<Tab.Screen
name={'Profile'}
component={ProfileScreen}
options={{
tabBarIcon: ({color}) => (
<Ionicons name={'person-outline'} size={24} />
),
}}
/>
</Tab.Navigator>
);
};
I'm new to react native and the navigation library as well. Thank you.
In the ProfileScreen, you are calling your thumbPress function immideately and also you are binding to this but it's not a class component. You should do something like this;
<TouchableOpacity onPress={() => thumbPress(post)}>
<Image source={{uri: post.images[0]}} style={styles.thumbnail} />
</TouchableOpacity>
Also your home screen name is "Home" , you are routing to the "HomeScreen".