I am relatively new in React Native and I tried to read a lot of tutorials everywhere, still I don't get the proper solution on my issue. What is the proper way of calling the action clearCart() and at the same time navigate? I am having this error regarding the dispatch when pressed the button.
It seems I use componentDidMount() or useEffect() and separately call the clearCart() but I would gladly welcome any suggestions as it will be additional for me.
//other imports
import {clearCart} from 'src/modules/cart/actions';
//other imports..
class WebviewPayment extends Component {
constructor(props, context) {
super(props, context);
const {route} = props;
this.state = {
loading: true,
uri: route?.params?.uri ?? '',
};
}
handleContinue = () => {
const {navigation, dispatch} = this.props;
dispatch(clearCart());
navigation.pop();
navigation.navigate(homeTabs.shop);
};
//other components here
render() {
const {loading, uri} = this.state;
const {t} = this.props;
return (
<ThemedView isFullView>
<WebView
source={{uri}}
onNavigationStateChange={data => this.handleResponse(data)}
style={styles.webView}
onLoadStart={() => this.setState({loading: false})}
/>
{loading && (
<View style={styles.viewLoading}>
<ActivityIndicator size="large" color="black"/>
</View>
)}
<Container style={styles.footer}>
<Button
title={t('cart:text_shopping')}
onPress={this.handleContinue}
/>
</Container>
</ThemedView>
);
}
}
const styles = StyleSheet.create({
webView: {
flex: 1,
backgroundColor: 'transparent',
},
viewLoading: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
justifyContent: 'center',
},
footer: {
marginVertical: margin.big,
},
});
WebviewPayment.propTypes = {};
export default withTranslation()(WebviewPayment);
In my actions.js, I have:
export function clearCart() {
return {
type: Actions.CLEAR_CART,
};
}
I got it working now and this might help others in case they come across with the same issue as mine. The working code is as follow:
//other imports
import {clearCart} from 'src/modules/cart/actions';
import {connect} from 'react-redux';
//other imports..
class WebviewPayment extends Component {
constructor(props, context) {
super(props, context);
const {route} = props;
this.state = {
loading: true,
uri: route?.params?.uri ?? '',
};
}
handleContinue = () => {
const {navigation} = this.props;
this.props.clearCart();
navigation.pop();
navigation.navigate(homeTabs.shop);
};
//other components here
render() {
const {loading, uri} = this.state;
const {t} = this.props;
return (
<ThemedView isFullView>
<WebView
source={{uri}}
onNavigationStateChange={data => this.handleResponse(data)}
style={styles.webView}
onLoadStart={() => this.setState({loading: false})}
/>
{loading && (
<View style={styles.viewLoading}>
<ActivityIndicator size="large" color="black"/>
</View>
)}
<Container style={styles.footer}>
<Button
title={t('cart:text_shopping')}
onPress={this.handleContinue}
/>
</Container>
</ThemedView>
);
}
}
const styles = StyleSheet.create({
webView: {
flex: 1,
backgroundColor: 'transparent',
},
viewLoading: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
justifyContent: 'center',
},
footer: {
marginVertical: margin.big,
},
});
WebviewPayment.propTypes = {};
const mapDispatchToProps = (dispatch) => ({
clearCart: () => dispatch(clearCart()),
});
export default connect(
null,
mapDispatchToProps,
)(withTranslation()(WebviewPayment));
I used react-redux connect then mapDispatchToProps to pass the clearCart() into props.
Related
I am newer for using react-native, and wanna try to create a camera with filter. I'm blocked in step to recognize face. Have success to draw rectangle when face detected, but the problem is once it goes out of detection. The camera stop running as it fixes on the last real-time capture
Here is my code:
import { useState, useEffect, useRef } from 'react'
import { Camera } from 'expo-camera'
import * as MediaLibrary from 'expo-media-library'
import { Text, StyleSheet, View, TouchableOpacity } from 'react-native'
import Button from './Button'
import { Ionicons } from '#expo/vector-icons'
import * as FaceDetector from 'expo-face-detector'
export default function PCamera() {
const cameraRef = useRef(undefined)
const [faceDetected, setFaceDetected] = useState([])
const [lastImage, setImage] = useState(undefined)
const [hasUsePermssion, setUsePermission] = useState(false)
const [type, switchToType] = useState(Camera.Constants.Type.front)
const takePicture = async () => {
if (cameraRef) {
try {
const options = {
quality: 1,
base64: true,
exif: false,
}
const data = await cameraRef.current.takePictureAsync(options)
setImage(data.uri)
console.log(data)
} catch (err) {
console.error(err)
}
}
}
const swithMode = () => {
switchToType(
type === Camera.Constants.Type.front
? Camera.Constants.Type.back
: Camera.Constants.Type.front
)
}
const handleFacesDetected = ({ faces }) => {
setFaceDetected(faces)
}
useEffect(() => {
;(async () => {
const { status } = await Camera.requestCameraPermissionsAsync()
if (status === 'granted') {
setUsePermission(true)
}
})()
}, [])
if (hasUsePermssion === null) {
return <View />
}
if (hasUsePermssion === false) {
return <Text>No access to camera</Text>
}
return (
<View style={styles.cameraContainer}>
<View style={styles.overlay}>
<Camera
ref={cameraRef}
style={styles.camera}
type={type}
onFacesDetected={handleFacesDetected}
faceDetectorSettings={{
mode: FaceDetector.FaceDetectorMode.fast,
detectLandmarks: FaceDetector.FaceDetectorLandmarks.all,
runClassifications:
FaceDetector.FaceDetectorClassifications.none,
minDetectionInterval: 100,
tracking: true,
}}
>
{faceDetected.length > 0 &&
faceDetected.map((face) => (
<View
key={face.faceID}
style={{
position: 'absolute',
borderWidth: 2,
borderColor: 'red',
left: face.bounds.origin.x,
top: face.bounds.origin.y,
width: face.bounds.size.width,
height: face.bounds.size.height,
}}
/>
))}
</Camera>
</View>
<View style={styles.optionsContainer}>
<View>
<TouchableOpacity onPress={swithMode}>
<Text>
<Ionicons
name="camera-reverse-outline"
size={24}
color="black"
/>
</Text>
</TouchableOpacity>
</View>
<Button
icon="camera"
title="Take Photo"
onPress={takePicture}
style={styles.button}
/>
<View>
<Text>...</Text>
</View>
</View>
</View>
)}
const styles = StyleSheet.create({
cameraContainer: {flex: 1,
},
overlay: {
flex: 6,
borderBottomStartRadius: 75,
borderBottomEndRadius: 75,
overflow: 'hidden',
},
camera: {
flex: 1,
},
optionsContainer: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
},
})
N.B: Don't take care of the Button, it's a custom component and works well
I'm not sure what happened, but I had to clean/rebuild gradle from a version issue with react-native-screens, and when I finally got everything back and working, the screens don't show on Android, but they work exactly as they did before with the issue. I'm not even sure how or where to look. I know it's the Drawer Navigator because if I pass a view instead it will show. Keep in mind - before I had the gradle issue, EVERYTHING worked - which is why I'm so stumped.
Here is my code:
App.tsx
import 'react-native-gesture-handler';
import React, { useEffect, useRef, useState } from 'react';
import { NavigationContainer, DefaultTheme, DarkTheme, LinkingOptions, NavigatorScreenParams, useNavigationContainerRef } from '#react-navigation/native';
import { createNativeStackNavigator, NativeStackHeaderProps } from '#react-navigation/native-stack';
import { Platform, StatusBar, View } from 'react-native';
import { enableScreens } from 'react-native-screens';
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context';
import Orientation from 'react-native-orientation-locker';
import OneSignal from 'react-native-onesignal';
import analytics from '#react-native-firebase/analytics';
import {
StyleSheet
} from 'react-native';
import MenuButton from './src/components/layout/MenuButton';
// Import pages and components
import { colors } from './src/styles/colorPalette';
import MainHeader from './src/components/headers/MainHeader';
import DrawerNavigator from './src/navigators/DrawerNavigator';
import ThemeContextProvider, { useTheme } from './src/contexts/ThemeContext';
import AuthContextProvider from './src/contexts/AuthContext'
import { useAuth } from './src/contexts/AuthContext';
import LoginRegister from './src/components/LoginRegister';
import EmailPhoneCollector from './src/components/EmailPhoneCollector';
import Search from './src/components/Search';
import SearchHeader from './src/components/headers/SearchHeader'
import DetailView from './src/components/DetailView';
import FullScreenLoader from './src/components/FullScreenLoading';
// Import theme and styling files
import { testAppDefaultTheme, testAppDefaultDarkTheme } from './src/styles/theme';
import Home from './src/pages/home';
// Globals (fonts, etc)
const sequel100Wide = Platform.OS == 'ios' ? 'Sequel100Wide-65' : 'Sequel100Wide-85'
type DrawerParamsList = {
Home: undefined
Videos: undefined
News: undefined
Podcasts: undefined
}
type RootStackParamList = {
Drawer: NavigatorScreenParams<DrawerParamsList>
DetailView: undefined
Search: undefined
config?: undefined
}
const Stack = createNativeStackNavigator()
enableScreens()
export default function App(props: any) {
// To override login functionality
const bypassLogin = false
const clearOnRestart = false
const CustomApp = () => {
const { clearID, logout, user, emailSMSId, timeExceeded } = useAuth()
const { darkMode, showNavBar } = useTheme()
const navigationRef = useNavigationContainerRef()
const routeNameRef = useRef<string | undefined>('')
// Where we display pre-prompt on login and iOS platform
const checkIosSoftPrompt = async () => {
const deviceState = await OneSignal.getDeviceState()
if (!deviceState?.isSubscribed) {
OneSignal.addTrigger('ios_prompt', 'true')
}
}
const linking: LinkingOptions<RootStackParamList> = {
prefixes: ['testApp://', 'https://testApp.com', 'https://*.testApp.com'],
config: {
screens: {
Drawer: {
initialRouteName: 'Home',
screens: {
Videos: 'tv',
News: 'news',
Podcasts: 'podcasts',
Home: ''
}
},
DetailView: {
path: ':category?/:type/:slug',
},
Search: {
path: 'search',
}
}
},
}
const HeaderComponent = (props: any) => <MainHeader {...props} />
useEffect(() => {
if (bypassLogin && user) logout()
/* if ((bypassLogin || (user || emailSMSId)) && Platform.OS == 'ios') {
checkIosSoftPrompt()
} */
// More aggressive targeting for Pre-Prompt message
if (Platform.OS === 'ios' && (emailSMSId)) checkIosSoftPrompt();
}, [bypassLogin, user, emailSMSId])
useEffect(() => {
if (clearOnRestart) clearID()
}, [])
const checkRoutes = async () => {
const previousRouteName = routeNameRef.current;
const currentRouteName = navigationRef.current?.getCurrentRoute()?.name ?? '';
if (previousRouteName !== currentRouteName) {
await analytics().logScreenView({ screen_name: currentRouteName });
}
// Save the current route name for later comparison
routeNameRef.current = currentRouteName;
}
const setRefs = () => {
routeNameRef.current = navigationRef.current?.getCurrentRoute()?.name;
}
return(
<SafeAreaProvider>
<StatusBar translucent barStyle={darkMode ? 'light-content' : 'dark-content'} backgroundColor="transparent" />
<NavigationContainer
ref={navigationRef}
onReady={ setRefs }
onStateChange={ checkRoutes }
theme={darkMode ? testAppDefaultDarkTheme : testAppDefaultTheme}
linking={linking}
fallback={<FullScreenLoader message='Loading...'/>}
>
{ bypassLogin || !!user || emailSMSId || (timeExceeded != null && !timeExceeded ) ?
<Stack.Navigator
screenOptions={{ header: HeaderComponent, contentStyle:{ backgroundColor: darkMode ? colors.background.darkHeaderBg : colors.background.lightSubdued } }}
>
<Stack.Screen name="Drawer" component={DrawerNavigator} options={{ headerShown: false }} />
{ /* Common screens outside flow of Drawer Navigator */}
<Stack.Screen name="Search" component={Search}
options={{
header: SearchHeader,
headerShown: false,
animation: 'fade_from_bottom',
headerTitle: '',
headerStyle: {
backgroundColor: darkMode ? colors.primary.blue7 : '#fff',
},
contentStyle: {
backgroundColor: darkMode ? colors.primary.blue7 : '#fff',
}
}}
/>
<Stack.Screen name="DetailView" component={DetailView}
options={{
presentation: 'containedModal',
headerShown: false,
contentStyle: {
backgroundColor: darkMode
? colors.primary.blue7
: '#fff',
},
orientation:
Platform.OS === 'ios' ? 'portrait_up' : undefined,
headerBackButtonMenuEnabled: false,
headerBackVisible: false,
headerTitle: ''
}}
/>
</Stack.Navigator>
:
<EmailPhoneCollector />
}
</NavigationContainer>
</SafeAreaProvider>
)
}
// Main app useEffect hook
useEffect(() => {
// Lock orientation with Orientation (rather than React Navigation)
Orientation.lockToPortrait()
// OneSignal initializer
OneSignal.setLogLevel(6, 0);
OneSignal.setAppId("99a9aeab-f82e-49e8-bc40-8f471c7bf1f9");
/* //Prompt for push on iOS
OneSignal.promptForPushNotificationsWithUserResponse((response: any) => { // Replace this for pre-prompt flow
console.log("Prompt response:", response);
}); */
//Method for handling notifications received while app in foreground
OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent => {
console.log("OneSignal: notification will show in foreground:", notificationReceivedEvent);
let notification = notificationReceivedEvent.getNotification();
console.log("notification: ", notification);
const data = notification.additionalData
console.log("additionalData: ", data);
// Complete with null means don't show a notification.
notificationReceivedEvent.complete(notification);
});
//Method for handling notifications opened
OneSignal.setNotificationOpenedHandler((notification) => {
// console.log("OneSignal: notification opened:", notification);
});
}, [])
return (
<AuthContextProvider>
<ThemeContextProvider>
<CustomApp />
</ThemeContextProvider>
</AuthContextProvider>
);
}
const styles = StyleSheet.create({
drawerIcon: {
width: 30,
height: 30,
}
})
DrawerNavigator:
import React, { useEffect, useState } from 'react';
import { Linking, View, TouchableOpacity, Platform } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import { SafeAreaView } from 'react-native-safe-area-context';
import FontAwesome from 'react-native-vector-icons/FontAwesome';
import { Divider, Switch } from 'react-native-elements';
import { DrawerContentScrollView, DrawerItem, DrawerItemList } from '#react-navigation/drawer';
import { colors } from '../../styles/colorPalette';
import { useTheme } from '../../contexts/ThemeContext';
import HomeLogo from './HomeLogo';
import { Button } from 'react-native-elements/dist/buttons/Button';
import Text from '../TextComponents'
// Contexts
import { useAuth } from '../../contexts/AuthContext'
import VersionCheck from 'react-native-version-check';
// Globals (fonts, etc)
const sequel100Wide6585 = Platform.OS == 'ios' ? 'Sequel100Wide-65' : 'Sequel100Wide-85'
export default function DrawerWrapper(props: any) {
const darkMode = useTheme().darkMode;
const { navigation, route, state } = props;
const { appInfo, user, logout } = useAuth();
const goToStore = async () => {
if (Platform.OS == 'ios') {
VersionCheck.getAppStoreUrl({ appID: "1609502829" })
.then(res => {
if (res) {
Linking.canOpenURL(res)
.then(supported => {
console.log('the link: ', res)
supported && Linking.openURL(res)
},
(err) => console.log('Error opening link: ', err)
)
}
})
}
else if (Platform.OS == 'android') {
VersionCheck.getPlayStoreUrl({ packageName: "com.testAppmobile.testApp" })
.then(res => {
if (res) {
Linking.canOpenURL(res)
.then(supported => {
console.log('the link: ', res)
supported && Linking.openURL(res)
},
(err) => console.log('Error opening link: ', err)
)
}
})
}
}
return (
<SafeAreaView style={{flex: 1}} edges={[]}>
<View style={{ flexDirection: "row", padding: 20, alignItems: 'center', alignContent: 'center', marginTop: Platform.OS == 'android' ? 20 : 0 }}>
<TouchableOpacity onPress={() => navigation.closeDrawer()}>
<Icon name="md-close" size={40} color={darkMode ? colors.primary.blue2 : 'black'}/>
</TouchableOpacity>
<HomeLogo />
<TouchableOpacity style={{flexGrow: 1, display: 'none'}}>
<FontAwesome style={{textAlign: 'right'}} name="cog" size={40} color={darkMode ? colors.primary.blue2 : 'black'} />
</TouchableOpacity>
</View>
{ user && <View style={{flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-end', paddingHorizontal: 20, paddingBottom: 10}}>
<Text style={{marginRight: 20}}>{user?.username ?? user?.name ?? user?.email}</Text>
<Button title="Logout" titleStyle={{fontSize: 12, textTransform: 'uppercase'}} style={{backgroundColor: 'red'}} onPress={() => logout()}/>
</View>}
<Divider />
<DrawerContentScrollView {...props}>
<DrawerItemList {...props} labelStyle={{fontFamily: sequel100Wide6585}}/>
<DrawerItem
onPress={() => Linking.openURL('https://testApp.com')}
activeBackgroundColor={darkMode ? colors.primary.blue5 : colors.primary.blue1} {...props}
label="testApp.com"
labelStyle={{fontFamily: sequel100Wide6585,
fontWeight: 'normal',
fontSize: 13,
paddingVertical: 15,
color: darkMode ? colors.primary.blue2 : '#000'}} icon={({focused, size, color}) => <FontAwesome size={30} style={{color: darkMode ? '#98D4FF' : "#000", marginLeft: -5}} name="external-link-square"/>}
/>
</DrawerContentScrollView>
<View style={{flexDirection: 'row', alignItems: 'center'}}>
{ !!appInfo?.appVersion && <Text variant="h5" style={{ color: darkMode ? '#fff' : '#000', paddingLeft: 10 }}>{`v${appInfo.appVersion}`}</Text> }
{ !!appInfo?.needsUpdate && <Button onPress={goToStore} buttonStyle={{borderRadius: 10, backgroundColor: 'green', marginLeft: 10}} title={<Text variant="h5" style={{color: '#fff'}}>Update Available</Text>} />}
</View>
</SafeAreaView>
)
}
My Home Stack:
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { ActivityIndicator, FlatList, Platform, RefreshControl, RefreshControlProps, StyleSheet, TouchableOpacity, View } from 'react-native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import { useHeaderHeight } from '#react-navigation/elements';
import PostCard from '../../components/PostCard';
import FullScreenLoader from '../../components/FullScreenLoading';
import { colors } from '../../styles/colorPalette';
import { useTheme } from '../../contexts/ThemeContext';
import LiveVideoCard from '../../components/LiveVideoCard';
import HorizontalCarousel from '../../components/HorizontalCarousel';
import { KeyboardAwareFlatList, KeyboardAwareFlatListProps } from 'react-native-keyboard-aware-scroll-view';
import FooterStack from '../../components/layout/FooterStack';
import PersonDetails from '../../components/PersonDetails';
import Text from '../../components/TextComponents';
import NoResults from '../../components/NoResults';
import Animated, { enableLayoutAnimations, FadeIn, Layout } from 'react-native-reanimated';
const Home = (props: any) => {
const { navigation } = props;
const darkMode = useTheme().darkMode;
const headerHeight = useHeaderHeight();
const Stack = createNativeStackNavigator();
const styles = StyleSheet.create({
button: {
backgroundColor: 'transparent',
borderRadius: 50,
flex: 1,
width: 120,
height: 40,
marginHorizontal: 10,
},
container: {
flexGrow: 1,
flex: 1,
},
image: {
height: 300,
resizeMode: 'contain',
},
logo: {
height: 100,
width: '100%',
resizeMode: 'contain',
},
posts: {
borderStyle: 'solid',
borderWidth: 5,
borderColor: 'red',
},
subtitle: {
fontSize: 12,
marginTop: 3,
color: '#777777',
flex: 1,
},
title: {
fontSize: 24,
fontWeight: 'bold',
flex: 1,
},
videoContainer: {
position: 'relative',
height: 300,
marginVertical: 25,
flex: 1,
},
watchMore: {
flexDirection: 'row',
justifyContent: 'space-between',
padding: 20,
borderTopWidth: 1,
borderBottomWidth: 1,
borderStyle: 'solid',
borderColor: 'rgba(0,0,0,0.25)',
},
});
const HomeHeader = React.memo(() => {
const darkMode = useTheme().darkMode
return (
<>
<LiveVideoCard />
<View>
<View style={[styles.watchMore, { backgroundColor: darkMode ? colors.background.darkBg : '#fff' }]}>
<Text style={{color: darkMode ? '#fff' : '#000', fontSize: 15, fontWeight: 'bold'}}>
Watch
</Text>
<TouchableOpacity onPress={() => navigation.navigate('Videos')}>
<Text style={{color: darkMode ? colors.primary.blue2 : colors.primary.blueMain, fontWeight: 'bold', fontSize: 14}}>
Watch More
</Text>
</TouchableOpacity>
</View>
<HorizontalCarousel />
</View>
</>
)
}, (prev, next) => {
return true
})
const FooterItem = React.memo(() => {
const darkMode = useTheme().darkMode
return(
<View style={{paddingBottom: 100}}>
<Text variant="h6" style={{alignSelf: 'center', color: darkMode ? '#fff' : '#000'}}>Loading more posts...</Text>
<ActivityIndicator size={'large'} color={darkMode ? '#fff' : '#777'} style={{ paddingVertical: 20 }} />
</View>
)}, () => true)
const renderItem = useCallback((props: any) => {
return (
<PostCard data={props.item.node} categoryOverlay="Home" index={props.index} navigation={navigation} />
)
}, [])
const CustomRefreshControl = (props: RefreshControlProps) => {
const darkMode = useTheme().darkMode
return <RefreshControl
progressViewOffset={headerHeight}
refreshing={props.refreshing}
onRefresh={props.onRefresh}
tintColor={darkMode ? '#fff' : '#000'}
titleColor={darkMode ? '#fff' : '#000'}
/>
}
const HomeComponent = useCallback(() => {
const [data, setData] = useState([]);
const flatlistRef = useRef<FlatList>(null)
const onEndReachedCalledDuringMomentum = useRef(false)
const [loading, setLoading] = useState(true);
const [pageNum, setPageNum] = useState(0);
const [refreshing, setRefreshing] = useState(false);
const controller = new AbortController()
const fetchData = async (newPageNum?: number) => {
if (newPageNum !== 0 && newPageNum == pageNum) return console.log("They're the same")
const timer = setTimeout(() => {
controller?.abort()
}, 10000)
try {
// Here is where we try to fetch data
const videos = await fetch(
`https://home.testApp.com/m/shows/latest?page=${newPageNum ?? pageNum}`,
{
method: 'GET',
mode: 'cors',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'testApp/2.0'
},
signal: controller.signal
}
);
const json = await videos
.json()
.catch((e: any) =>
console.log('this is an error in the json output: ', e),
);
clearTimeout(timer)
setData(data.concat(json.nodes));
if (!!newPageNum) setPageNum(newPageNum)
} catch (e) {
// Manage the errors here
console.log('Error returning from testApp videos: ', e);
} finally {
if (newPageNum === 0 || !!loading) setLoading(false)
if (!!refreshing) setRefreshing(false)
controller?.abort()
}
};
const keyExtractor = (item: any, idx: number) => {
return `${item.node.nid}-${idx}`
};
const handleLoadMore = async (props: any) => {
if (onEndReachedCalledDuringMomentum.current === true) return console.log('momentum blocked')
fetchData(pageNum + 1)
onEndReachedCalledDuringMomentum.current = true
}
const handleRefresh = async () => {
setRefreshing(true);
setLoading(true);
setData([]);
fetchData(0);
setRefreshing(false)
}
const _onMomentumScrollBegin = () => {
onEndReachedCalledDuringMomentum.current = false
}
useEffect(() => {
// This is where we fetch the data from the website
if (loading) fetchData();
// Pass ref to navigation object for scrollTo logic
navigation.setOptions({
homeScrollRef: flatlistRef
})
return () => controller?.abort()
}, []);
if (!!loading) return <FullScreenLoader message={'Loading Home posts'} />
return (
<FlatList
ref={flatlistRef}
contentContainerStyle={{marginTop: headerHeight ?? undefined }}
style={{flex: 1 }}
scrollIndicatorInsets={{ right: 1 }}
showsVerticalScrollIndicator={false}
initialNumToRender={5}
maxToRenderPerBatch={15}
windowSize={15}
data={data}
keyExtractor={keyExtractor}
ListEmptyComponent={<NoResults />}
ListHeaderComponent={HomeHeader}
ListFooterComponent={FooterItem}
renderItem={renderItem}
onEndReached={handleLoadMore}
onEndReachedThreshold={0.5}
onMomentumScrollBegin={_onMomentumScrollBegin}
scrollEventThrottle={16}
refreshControl={<CustomRefreshControl
refreshing={refreshing}
onRefresh={handleRefresh}
/>}
/>
)
}, [])
return (
<Stack.Navigator screenOptions={{ orientation: Platform.OS === 'ios' ? 'portrait_up' : 'portrait_up', headerShown: false, contentStyle: { backgroundColor: darkMode ? colors.background.darkHeaderBg : colors.background.lightSubdued }}}>
<Stack.Screen name="HomeView" options={{title: 'Home' }} component={HomeComponent} />
<Stack.Screen name="FooterLinks" component={FooterStack} />
</Stack.Navigator>
);
}
export default Home
try disable animation on android
import { enableLayoutAnimations } from "react-native-reanimated";
if (Platform.OS === 'android') {
enableLayoutAnimations(false);
}
I finished a React Native course, and I'm trying to make a chat app to practice.
Summarize the problem:
I have 2 screens ,ContactList.js and ChatRoom.js
I have a Navigation stack with these two screens Navigation.js
The Navigation component is imported and rendered in App.js
I added FCM module to handle notifications
The goal is to execute the function that loads messages in the chatroom _loadMessages(), when the app receives a notification on foreground state. And to execute the function (I didn't create it yet) to update unread message in a global state.
What I've tried
I followed react native firebase docs, I have a function that handle notification on foreground declared inside App.js. The problem is that I can't tell the other components (the screens) to execute their functions. The "Ref" method can't be used cause I'm not calling the child component (the screens) directly inside the App.js, I'm calling and rendering the Navigation.js Stack instead.
So, in this case, when we have a navigation component called on app.js, how can we tell other components to execute a function that is declared inside them?
App.js
import React, { useEffect } from 'react'
import Navigation from './Navigation/Navigation'
import messaging from '#react-native-firebase/messaging';
export default function App() {
requestUserPermission = async () => {
//On récupere le token
const token = await messaging().getToken();
console.log('TOKEN: ' + token)
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
}
}
handleForegroundNotification = () => {
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log('A new FCM message arrived!', JSON.stringify(remoteMessage));
});
return unsubscribe;
}
useEffect(() => {
this.requestUserPermission();
this.handleForegroundNotification();
}, []);
return (
<Navigation />
)
}
Navigation.js
import { createAppContainer } from "react-navigation"
import { createStackNavigator } from "react-navigation-stack"
import ContactList from '../Components/ContactList'
import ChatRoom from '../Components/ChatRoom'
const ChatStackNavigator = createStackNavigator({
ContactList: {
screen: ContactList,
navigationOptions: {
title: 'Contacts'
}
},
ChatRoom: {
screen: ChatRoom,
navigationOptions: {
title: 'Conversation'
}
}
})
export default createAppContainer(ChatStackNavigator)
ChatRoom.js
import React from 'react'
import { View, StyleSheet, Text, Image, SafeAreaView, TextInput, Alert, FlatList, ActivityIndicator } from 'react-native'
import { sendMessage } from '../API/sendMessageApi'
import { getMessages } from '../API/getMessagesApi'
import MessageItem from './MessageItem'
class ChatRoom extends React.Component {
constructor(props) {
super(props)
this.message = ""
this.contact = this.props.navigation.getParam('contact')
this.state = {
defautInputValue: "",
listMessages: [],
isLoading: true
}
}
_textInputChanged(text) {
this.message = text
}
_sendMessage() {
this.setState({ defautInputValue: " " });
sendMessage('1', this.contact.id_contact, this.message).then(() => {
this._loadMessages();
});
}
_loadMessages() {
getMessages('1', this.contact.id_contact).then((data) => {
this.setState({ listMessages: data, isLoading: false, defautInputValue: "" })
});
}
componentDidMount() {
this._loadMessages();
}
_displayLoading() {
if (this.state.isLoading) {
return (
<View style={[styles.loading_container]}>
<ActivityIndicator size="large" color="orange" />
</View>
)
}
}
render() {
//console.log('Contact ID: ' + JSON.parse(this.contact))
return (
<SafeAreaView style={styles.container}>
<View style={styles.contact}>
<View style={styles.image_container}>
<Image
style={styles.image}
source={{ uri: 'https://moonchat.imedramdani.com/avatar/' + this.contact.avatar }}
></Image>
</View>
<View style={styles.username_container}>
<Text style={styles.username}>{this.contact.username}</Text>
</View>
</View>
{/* BODY */}
<View style={styles.body}>
<FlatList
ref={ref => this.flatList = ref}
onContentSizeChange={() => this.flatList.scrollToEnd({ animated: true })}
data={this.state.listMessages}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) =>
<MessageItem
message={item}
/>}
>
</FlatList>
</View>
<View style={styles.input_container}>
<TextInput
style={styles.input}
onChangeText={(text) => this._textInputChanged(text)}
onSubmitEditing={() => this._sendMessage()}
defaultValue={this.state.defautInputValue}
placeholder="Aa"
></TextInput>
</View>
{this._displayLoading()}
</SafeAreaView>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#1d2733',
},
contact: {
height: 50,
backgroundColor: '#1d2733',
marginTop: 0,
flexDirection: 'row',
borderBottomColor: 'grey',
borderWidth: 1
},
username_container: {
//backgroundColor: 'red',
flex: 3,
justifyContent: 'center',
alignItems: 'flex-start'
},
username: {
fontSize: 20,
color: 'white'
},
image_container: {
//backgroundColor: 'blue',
flex: 1,
justifyContent: 'center'
},
image: {
//backgroundColor: 'yellow',
height: 45,
width: 45,
marginLeft: 10,
borderRadius: 25
},
body: {
//backgroundColor: 'red',
flex: 1
},
input_container: {
height: 75,
//backgroundColor:'blue',
padding: 5
},
input: {
paddingLeft: 20,
height: 50,
backgroundColor: 'white',
borderWidth: 1,
borderRadius: 25,
borderColor: '#D5D5D5',
fontSize: 20
},
loading_container: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
alignItems: 'center',
justifyContent: 'center'
},
});
export default ChatRoom
Thanks!
I set up a solution that worked, but I don't know if it is a proper way.
I restored App.js
import React from 'react'
import Root from './Root'
import { Provider } from 'react-redux'
import Store from './Store/configureStore'
class App extends React.Component {
render() {
return (
<Provider store={Store}>
<Root />
</Provider>
)
}
}
export default App
I created a now component Root.js which contains notification handler
import React from 'react'
import Navigation from './Navigation/Navigation'
import messaging from '#react-native-firebase/messaging'
import { connect } from 'react-redux'
class Root extends React.Component {
requestUserPermission = async () => {
//On récupere le token
const token = await messaging().getToken();
console.log('TOKEN: ' + token)
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
}
}
handleForegroundNotification = () => {
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log('A new FCM message arrived!', JSON.stringify(remoteMessage));
const action = { type: "RELOAD_MESSAGES", value: '1' }
this.props.dispatch(action)
});
return unsubscribe;
}
componentDidMount() {
this.requestUserPermission();
this.handleForegroundNotification();
}
render() {
return (
<Navigation />
)
}
}
const mapStateToProps = (state) => {
return {
loadyourself: state.loadyourself
}
}
export default connect(mapStateToProps)(Root)
The store is provided in App.js to let Root.js access the global state.
When a notification is received at foreground, the Root.js update a key in the global state named "loadyourself". When the state is updated, the ChatRoom.js which is connected to the store too, trigger the componentDidUpdate()
componentDidUpdate() {
if (this.props.loadyourself == "1") {
this._reloadMessages();
}
}
of course, to avoid the infinite loop, the _reloadMessages() restore default value in the global state of loadyourself key
_reloadMessages() {
const action = { type: "RELOAD_MESSAGES", value: '0' }
this.props.dispatch(action)
getMessages('1', this.contact.id_contact).then((data) => {
this.setState({ listMessages: data })
});
}
The messages are updated, the global state is re-initialized, the componentDidUpdate() does not trigger until next notification.
It works. Like I said, I don't know if there is a more proper way, I'm new in React-Native (2 weeks). I am open to other solutions
I am implementing video playing app(like Instagram Reels or tik tok) using RecyclerListView in React-Native. In the development of app, I am facing the problem that all the videos in the list play simultaneously. I want to pause all other videos which are out of the screen and play only when scrolled to a particular video and video is visible on screen.
How to do it? I have tried a lot of things but could not find solution for RecyclerListView.
I have used react-native-video for playing video.
App.js
import VideoPlayer from './ViewVideo';
const fakeData = [
{
type: 'NORMAL',
item: {
uri: require('./images/likd2.mp4'),
},
},
{
type: 'NORMAL',
item: {
uri: require('./images/Linkd.mp4'),
},
},
{
type: 'NORMAL',
item: {
uri: require('./images/PlayDate.mp4'),
},
},
];
export default class Myworld extends React.Component {
dataProvider = new DataProvider((r1, r2) => {
return r1 !== r2;
}).cloneWithRows(fakeData);
layoutProvider = new LayoutProvider(
(i) => {
return this.dataProvider.getDataForIndex(i).type;
},
(type, dim) => {
switch (type) {
case 'NORMAL':
dim.width = '100%';
dim.height = '100%';
break;
default:
dim.width = '100%';
dim.height = '100%';
break;
}
},
);
rowRenderer = (type, data, index) => {
switch (type) {
case 'NORMAL':
return (
<View>
<VideoPlayer source={uri} />
</View>
);
}
};
render() {
return (
<>
<SafeAreaView style={styles.container}>
<RecyclerListView
style={styles.videolistcontainer}
rowRenderer={this.rowRenderer}
dataProvider={this.dataProvider}
layoutProvider={this.layoutProvider}
initialOffset={1}
pagingEnabled={true}
showsVerticalScrollIndicator={false}
/>
</SafeAreaView>
</>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
backgroundColor: '#14171A',
},
});
ViewVideo.js
const VideoPlayer = (props) => {
const [paused, setPause] = useState(false);
return (
<>
<TouchableOpacity
style={{height: height, width: width}}
onPress={() => setPause(!paused)}>
<Video
ref={(ref) => {
setVideoRef(ref);
}}
source={props.source}
style={styles.backgroundVideo}
resizeMode={'cover'}
onError={onError(videoRef)}
paused={paused}
onLoad={onLoad}
onProgress={onProgress}
onEnd={onEnd}
repeat={false}
rate={1.0}
volume={1.0}
muted={false}
onLayout={onVideoLayout}
/>
</TouchableOpacity>
</>
);
};
export default VideoPlayer;
const styles = StyleSheet.create({
backgroundVideo: {
position: 'absolute',
width: WP('100%'),
height: HP('100%'),
left: 0,
right: 0,
top: 0,
bottom: 0,
},
});
You can use AppState of React-Native.
It indicate the future state of app: active, background or inactive(IOS only)
Example:
import React, { useRef, useState, useEffect } from "react";
import { AppState, Text, View } from "react-native";
const AppInactiveHandleExample = () => {
const appState = useRef(AppState.currentState);
const [appStateVisible, setAppStateVisible] = useState(appState.current);
useEffect(() => {
AppState.addEventListener("change", handleAppStateChange);
// Return for clear the cache of action
return () => {
AppState.removeEventListener("change", handleAppStateChange);
};
}, []);
const handleAppStateChange = nextAppState => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === "active"
) {
// Execute here your action
onAppInactive()
}
appState.current = nextAppState;
setAppStateVisible(appState.current);
};
// Action executed when app was inactive or background
const onAppInactive = () => {
// Your code here
}
return (
<View>
<Text>Current state is: {appStateVisible}</Text>
</View>
);
};
export default AppInactiveHandleExample;
https://reactnative.dev/docs/appstate
I want to set styling for a custom component, inside navigationOptions in react native. But the stying doesnt work and it give an error saying. Same styling is working in another text box of the same screen.
P:S: I could achieve this by doing this? Am I doing this correct? Is this the proper way of handling this?
class WorkoutScreen extends Component {
constructor(props) {
super(props);
this.state = {
searchText: ""
};
}
componentDidMount() {
this.props.navigation.setParams({
searchWorkouts: this.searchWorkoutHandler,
onChangeSearchText: this.onChangeSearchTextHandler,
searchText: this.state.searchText
});
}
// on change search text
onChangeSearchTextHandler = value => {
this.setState({
searchText: value
});
this.props.navigation.setParams({
searchText: value
});
};
// search workouts
searchWorkoutHandler = () => {
alert("Searching Workouts");
};
render() {
return (
<View style={styles.container}>
<Text>Im Here</Text>
</View>
);
}
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
headerTitle: (
<SearchInput
style={styles.searchInput}
value={params.searchText}
source={Search}
borderRadius={50}
placeholder="Search / Filter"
onChangeText={value => params.onChangeSearchText(value)}
onPress={() => params.searchWorkouts()}
/>
),
headerTitleStyle: { width: "100%", alignItems: "center" },
headerStyle: {
paddingRight: 10,
paddingLeft: 10
},
headerLeft: (
<ClickableIcon
source={Bookmark}
onIconPressed={() => alert("Notifications Clicked Workout")}
/>
),
headerRight: (
<ClickableIcon
source={Movements}
onIconPressed={() => alert("Add New Clicked")}
/>
)
};
};
}
const styles = StyleSheet.create({
container: {
flex: 1
},
scrollView: {
backgroundColor: "#ffffff"
},
searchInput: {
height: 45,
color: "gray",
fontSize: 18
}
});
export default WorkoutScreen;
How can I overcome this?