ListView doesn't scroll - react-native

I have a horizontal ListView (shown in the image with list items a,b,c) that will not scroll, or rather, it does scroll - but one out of 10 or so swipes seems to make it scroll. I have also used a FlatList - same result. The ListView is on a Interactable.View, however I got rid of the Interactable.View and it still didn't scroll. I've tested on a real ios device and a genymotion emulator and both had the same result. There are more items in the list than just a,b,c. There are 6 items in the list.
import { StyleSheet, View, Text, FlatList, ListView } from 'react-native'
import React, { Component } from 'react'
import MapView from 'react-native-maps'
import { connect } from 'react-redux'
import {
Button,
Container
} from 'native-base'
import { updateRegion } from './map.action'
import { OptimizedFlatList } from 'react-native-optimized-flatlist'
import Icon from 'react-native-vector-icons/FontAwesome'
import { toggleMenu } from '../search-page/searchPage.action'
import mapStyle from './style'
import Interactable from 'react-native-interactable'
import { setSelectedShop } from '../search-results/searchResults.action'
import { updateHeight } from '../search-results/searchResultsPresenter.action'
import { getSelectedProduct } from './markers.selector'
const mapStateToProps = (state) => ({
region: state.get('map').get('region'),
markers: state.get('searchResults').get('products'),
selectedProduct: getSelectedProduct(state),
height: state.get('searchResultsPresenter').get('height')
})
const mapDispatchToProps = (dispatch) => ({
onRegionChange: (region) => {
dispatch(updateRegion(region))
},
onToggleMenuClick: () => {
dispatch(toggleMenu())
},
setSelectedShop: id => {
dispatch(setSelectedShop(id))
},
updateHeight: height => {
dispatch(updateHeight(height))
}
})
class Map extends Component {
componentDidMount() {
const { store } = this.context
this.unsubscribe = store.subscribe(() => { })
}
componentWillUnmount() {
this.unsubscribe()
}
componentWillReceiveProps(newProps) {
if (newProps.selectedProduct) {
let products = newProps.selectedProduct.products
this.setState({
dataSource: this.state.dataSource.cloneWithRows(products)
})
}
}
interactableView;
constructor(props) {
super(props)
this.state = { dataSource: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }) }
}
render() {
console.log(this.props.height)
return (
<Container>
<MapView
style={styles.map}
region={this.props.region}
onRegionChangeComplete={this.props.onRegionChange}>
{
this.props.markers.map(marker => {
return (
<MapView.Marker
coordinate={marker.shop.coordinate}
title={marker.shop.name}
identifier={marker.shop.id.toString()}
onPress={e => {
console.log(e.nativeEvent)
this.interactableView.snapTo({ index: 0 })
this.props.setSelectedShop(marker.shop)
console.log(this.props.selectedProduct)
}}
/>
)
})
}
</MapView>
<Button
small
icon
style={mapStyle.toggleMenuButton}
onPress={() => this.props.onToggleMenuClick()}>
<Icon name="sliders" size={20} color="#FFFFFF" />
</Button>
<Interactable.View
style={{
flex: 1,
flexDirection: 'row',
zIndex: 20,
borderRadius: 10,
backgroundColor: '#222222',
padding: 30,
paddingTop: 50
}}
verticalOnly={true}
snapPoints={[{ y: this.props.height - 225 }, { y: this.props.height - 50 }]}
initialPosition={{ y: this.props.height - 50 }}
ref={view => this.interactableView = view}
onLayout={(event) => {
this.props.updateHeight(event.nativeEvent.layout.height)
}} >
<View style={{ flex: 1, flexDirection: 'row', height: 50 }}>
<Text
style={{
color: 'white',
position: 'absolute',
top: -40,
marginBottom: 20,
textAlign: 'center',
width: '100%'
}}>
{this.props.selectedProduct ? this.props.selectedProduct.shop.name : ''}
</Text>
<ListView
dataSource={this.state.dataSource}
horizontal={true}
style={{
height: 200
}}
renderRow={(rowData)=> {
console.log(rowData)
return (
<View style={{
backgroundColor: 'blue',
width: 100,
borderWidth: 1,
borderColor: 'black',
margin: 0
}}>
<Text style={{ color: 'white' }}>{rowData.name}</Text>
<View style={{
zIndex: 15,
width: '100%',
height: '100%',
backgroundColor: 'red'
}}>
</View>
</View>
)
}}
/>
</View>
</Interactable.View>
</Container>
)
}
}
Map.contextTypes = {
store: React.PropTypes.object
}
Map.propTypes = {
region: React.PropTypes.shape({
latitude: React.PropTypes.number,
longitude: React.PropTypes.number,
latitudeDelta: React.PropTypes.number,
longitudeDelta: React.PropTypes.number
}).isRequired,
height: React.PropTypes.number,
updateHeight: React.PropTypes.func,
setSelectedShop: React.PropTypes.func,
selectedProduct: React.PropTypes.string,
onRegionChange: React.PropTypes.func.isRequired,
onToggleMenuClick: React.PropTypes.func.isRequired,
markers: React.PropTypes.array
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Map)
const styles = StyleSheet.create({
map: {
...StyleSheet.absoluteFillObject,
zIndex: 3
}
})
How do I get the listview to scroll with every swipe, rather than about 1 out of 10 swipes?
Same thing happens when I use a ScrollView. So nothing can be swiped horizontally there. Even when removing the interactable.view

Making renderRow return a TouchableHighlight instead of a View caused the list to be draggable with every touch and swipe. And finally, unessential to the answer, I switched from a ListView to a FlatList because ListView is going to be deprecated in the future. I felt it good to state that as at this point in time it isn't clear from the official documentation that ListView is becoming deprecated.

Related

BottomSheet (#gorhom/bottom-sheet) doesn't drag the sheet up or down (iOS Only)

Sometimes BottomSheet doesn't showing, I think it happens because of conflict with. When I build release APK and install it on IOS, when you open application first time then bottom sheet doesn't show but if you close and reopen application then bottom sheet appears. Had anyone this problem? This issue not appear in android, it issue only in IOS
import React, {
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from 'react';
import {
Text,
StyleSheet,
Image,
Linking,
TouchableOpacity,
View,
Platform,
} from 'react-native';
import { Box, AbstractButton } from '../../components';
import { COLORS, assets, FONTS, FONT_WEIGHT, SIZES } from '../../constants';
import { RootStackParamList } from '../../navigation/types';
import { NativeStackScreenProps } from '#react-navigation/native-stack';
import { useSelector } from 'react-redux';
import { RootState } from '../../state/rootReducer';
import { useAppDispatch } from '../../state/index';
import { setProfilePhoto, setUserId } from '../../state/onboarding';
import { setIsBottomSheet, setIsEnableHeader } from '../../state/generalUtil';
import BottomSheet, { BottomSheetView } from '#gorhom/bottom-sheet';
import ProfilePhotoBottomSheet from './ProfilePhotoBottomSheet';
import { useBackHandler, useDimensions } from '#react-native-community/hooks';
import {
addDoitLater,
createUserOnboarding,
getDoitLater,
getUserDataById,
uploadProfileImage,
} from '../../service/OnboardingService';
import {
setBarStyle,
setStatusbarColor,
setTranslucent,
} from '../../state/generalUtil';
import { CustomCamera } from '../../components/CameraScreen';
// import { launchCamera, launchImageLibrary } from 'react-native-image-picker';
import ApiUtil from '../../util/ApiUtil';
import { CommonActions } from '#react-navigation/native';
import ImagePicker from 'react-native-image-crop-picker';
import PermissionService from '../../service/PermissionService';
import { InfoPopUp } from '../../components/PopUpModal';
import CameraInfoPopup from './CameraInfoPopup';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
const ProfilePhoto = ({
route,
navigation,
}: NativeStackScreenProps<RootStackParamList>) => {
const { profilePhoto, userId } = useSelector(
(state: RootState) => state.onboarding,
);
const { PERMISSIONS_TYPE, checkPermissions, requestPermisssions } =
PermissionService;
const [isSelected, setIsSelected] = useState(false);
const [openCamera, setOpenCamera] = useState(false);
const [visible, setVisible] = useState(false);
const dispatch = useAppDispatch();
const { screen, window } = useDimensions();
// var ImagePicker = NativeModules.ImageCropPicker;
useEffect(() => {
if (openCamera) {
dispatch(setStatusbarColor('#000000'));
dispatch(setBarStyle('light-content'));
dispatch(setIsBottomSheet(true));
dispatch(setTranslucent(false));
dispatch(setIsEnableHeader(false));
} else {
dispatch(setStatusbarColor('#F2F2EF'));
dispatch(setBarStyle('dark-content'));
dispatch(setIsBottomSheet(false));
dispatch(setTranslucent(false));
}
}, [openCamera, dispatch]);
// BottomSheet Hooks
const sheetRef = useRef<BottomSheet>(null);
// Keep Tracking bootom sheet is open or not.
const [isOpen, setIsOpen] = useState(false);
// BottomSheet snap variables
const snapPoints = ['38%'];
// BottomSheet Callbacks
const handleClosePress = useCallback(() => {
sheetRef.current?.close();
}, []);
const handleCloseModal = useCallback(() => {
setVisible(false);
}, []);
const UpdateProfilePhoto = async () => {
const userData: any = await getUserDataById();
if (userData?.basicInfo?.userId) {
await uploadProfileImage(profilePhoto.url, userData?.basicInfo?.userId);
dispatch(setUserId(userData.basicInfo?.userId));
}
};
// BottomSheet Callbacks
const handleSnapPress = useCallback(index => {
sheetRef.current?.snapToIndex(index);
setIsOpen(true);
}, []);
const handleTakePhoto = async () => {
const PermissionStatus = await checkPermissions(PERMISSIONS_TYPE.camera);
if (PermissionStatus === 'blocked') {
setVisible(true);
}
const permission = await requestPermisssions(PERMISSIONS_TYPE.camera);
if (permission === 'granted') {
setOpenCamera(true);
}
};
const handleCloseCam = (image?: any) => {
if (image) {
dispatch(setProfilePhoto(image.uri));
}
setOpenCamera(false);
setIsOpen(false);
};
const handleSelectPhoto = () => {
ImagePicker.openPicker({
width: 300,
height: 400,
mediaType: 'photo',
}).then(async image => {
setIsSelected(true);
dispatch(setProfilePhoto(image.path));
});
};
const handleContinue = () => {
if (userId) {
UpdateProfilePhoto();
createUserOnboarding(userId, 10);
}
if (route?.params?.doItLaterFlag === 10) {
navigation.dispatch(
CommonActions.reset({
index: 0,
routes: [{ name: 'ThingsToDoScreen' }],
}),
);
} else {
navigation.navigate('ConfirmDetails');
}
};
const doItLater = async () => {
dispatch(setProfilePhoto(''));
if (route?.params?.doItLaterFlag === 10) {
navigation.dispatch(
CommonActions.reset({
index: 0,
routes: [{ name: 'ThingsToDoScreen' }],
}),
);
} else {
navigation.navigate('ConfirmDetails');
}
};
useBackHandler(
useCallback(() => {
if (isOpen) {
sheetRef.current?.close();
} else {
navigation.goBack();
}
return true;
}, [isOpen]),
);
return (
<>
{openCamera ? (
<Box width={window.width} height={window.height - 40}>
<CustomCamera type={'front'} close={handleCloseCam} />
</Box>
) : (
<>
<Box style={isOpen ? styles.containerOpacity : styles.container}>
<InfoPopUp visible={visible} handleCloseModal={handleCloseModal}>
{<CameraInfoPopup handleCloseModal={handleCloseModal} />}
</InfoPopUp>
<Box>
<Box
pt={145}
flexDirection={'column'}
style={{ alignItems: 'center' }}>
<TouchableOpacity
disabled={isOpen}
onPress={() => {
handleSnapPress(1);
}}>
<View style={styles.photoView}>
{profilePhoto.url ? (
<>
<Box>
<Image
source={{ uri: profilePhoto.url }}
style={styles.image}
/>
</Box>
<Box style={styles.insideImg3}>
<Image
source={assets.EditPen}
style={styles.PlusIconImage}
/>
</Box>
</>
) : (
<>
<Box style={styles.insideImg1}>
<Image
source={assets.Smile}
style={styles.SmileImage}
/>
</Box>
<Box style={styles.insideImg2}>
<Image
source={assets.PlusIcon}
style={styles.PlusIconImage}
/>
</Box>
</>
)}
</View>
</TouchableOpacity>
<Text style={styles.text}>Add profile photo</Text>
<Text style={styles.text1}>
To promote a safe and transparent community, we recommend a
clear photo of yourself
</Text>
</Box>
</Box>
</Box>
<Box style={styles.continueBtn}>
<AbstractButton
disabled={!profilePhoto.url}
onPress={handleContinue}>
Continue
</AbstractButton>
<TouchableOpacity onPress={doItLater}>
<Text style={styles.text2}>Do it later</Text>
</TouchableOpacity>
</Box>
{/* Bottom Sheet */}
{isOpen && (
<BottomSheet
ref={sheetRef}
snapPoints={snapPoints}
enablePanDownToClose={true}
onClose={() => setIsOpen(false)}
backgroundStyle={{
backgroundColor: COLORS.background.primary,
borderColor: COLORS.gray,
borderWidth: 2,
}}>
<GestureHandlerRootView>
<BottomSheetView>
<ProfilePhotoBottomSheet
route={route}
navigation={navigation}
handleTakePhoto={handleTakePhoto}
handleSelectPhoto={handleSelectPhoto}
handleClosePress={handleClosePress}
/>
</BottomSheetView>
</GestureHandlerRootView>
</BottomSheet>
)}
</>
)}
</>
);
};
const styles = StyleSheet.create({
continueBtn: {
position: 'absolute',
justifyContent: 'center',
alignItems: 'center',
alignSelf: 'center',
width: '100%',
paddingHorizontal: 10,
top:
Platform.OS === 'ios'
? SIZES.screen_height / 1.4
: SIZES.screen_height / 1.35,
},
container: {
flex: 1,
paddingHorizontal: 20,
backgroundColor: COLORS.background.primary,
},
containerOpacity: {
flex: 1,
opacity: 0.5,
paddingHorizontal: 20,
// backgroundColor: COLORS.lightGray,
},
container1: {
backgroundColor: COLORS.background.primary,
alignItems: 'center',
flexDirection: 'row',
justifyContent: 'space-between',
paddingHorizontal: 20,
},
SmileImage: {
width: 65,
height: 64,
},
PlusIconImage: {
width: 40,
height: 40,
},
text: {
paddingTop: '10%',
color: COLORS.black,
fontSize: SIZES.extraLarge,
fontFamily: FONTS.PlayfairDisplayBold,
lineHeight: 30,
alignItems: 'center',
},
text1: {
fontSize: SIZES.font,
fontFamily: FONTS.MerriweatherRegular,
lineHeight: 24,
color: '#2B2928',
paddingHorizontal: '15%',
paddingVertical: '4%',
textAlign: 'center',
},
text2: {
paddingTop: '8%',
color: COLORS.black,
fontFamily: FONTS.MerriweatherBold,
fontSize: 13,
lineHeight: 21,
textAlign: 'center',
},
photoView: {
width: 150,
height: 150,
backgroundColor: COLORS.white,
borderRadius: 100,
},
insideImg1: {
top: '30%',
left: '28%',
},
insideImg2: {
top: '30%',
left: '70%',
},
insideImg3: {
top: '-30%',
left: '70%',
},
image: {
width: 150,
height: 150,
borderRadius: 100,
},
});
export default ProfilePhoto;
Update: Different similar issue but on Android
I was also having this exact problem, my BottomSheetModals and BottomSheetScrollViews were not responding to touch events on Android.
Root Cause
react-native-gesture-handler requires a wrapper around the root view to function. In v1 of the library, this was done on the native side, and expo did this for you. In v2 of the library, you must use the GestureHandlerRootView in your app manually. Upgrading to SDK 44 of expo removes the native RNGH setup.
FIX
The GestureHandlerRootView must be applied as high as possible in your app's component tree.
In my case, I had my BottomSheetModalProvider outside the GestureHandlerRootView, and swapping these two components fixed the issue for me!
Before:
<BottomSheetModalProvider>
<GestureHandlerRootView style={{ flex: 1 }}>
<MainNavigation />
</GestureHandlerRootView>
</BottomSheetModalProvider>
After:
<GestureHandlerRootView style={{ flex: 1 }}>
<BottomSheetModalProvider>
<MainNavigation />
</BottomSheetModalProvider>
</GestureHandlerRootView>
#gorhom does it make sense to update the docs to say that BottomSheetModalProvider must be inside GestureHandlerRootView?
make sure to wrap your whole app with
<GestureHandlerRootView style={{flex:1}}>
{your other components}
</GestureHandlerRootView>
and read the gesture handler docs for installation and setup tips if this doesn't solve your issue
you might also need to add import 'react-native-gesture-handler'; at the beginning of your index.js
Finally, I got a solution for this issue. don't wrap with Ternary operator the bottomsheet. pls reffer below code.
Before :
{isOpen && (
<BottomSheet
ref={sheetRef}
snapPoints={snapPoints}
enablePanDownToClose={true}
onClose={() => setIsOpen(false)}
backgroundStyle={{
backgroundColor: COLORS.background.primary,
borderColor: COLORS.gray,
borderWidth: 2,
}}>
<GestureHandlerRootView>
<BottomSheetView>
<ProfilePhotoBottomSheet
route={route}
navigation={navigation}
handleTakePhoto={handleTakePhoto}
handleSelectPhoto={handleSelectPhoto}
handleClosePress={handleClosePress}
/>
</BottomSheetView>
</GestureHandlerRootView>
</BottomSheet>
)}
</>
)}
After fix :
<BottomSheet
index={-1}
ref={sheetRef}
snapPoints={snapPoints}
enablePanDownToClose={true}
onClose={() => setIsOpen(false)}
backgroundStyle={{
backgroundColor: COLORS.background.primary,
borderColor: COLORS.gray,
borderWidth: 2,
}}>
<BottomSheetView>
<ProfilePhotoBottomSheet
route={route}
navigation={navigation}
handleTakePhoto={handleTakePhoto}
handleSelectPhoto={handleSelectPhoto}
handleClosePress={handleClosePress}
/>
</BottomSheetView>
</BottomSheet>
```

How to remove/replace default Profile Picture with Image Picker In React Native Expo

I am looking for assistance on how to replace a default profile picture with the one a user would select from their media library.
I have managed to create an onPress function that allows the user to select the image from their media library. The image is returned and displayed also in the prescribed layout.
My problem is that I cannot see the default profile picture, but I can see and click on a pencil icon to prompt the image-picker for iOS and Android as I am using Expo.
Here is my custom component code:
import React, { useState, useEffect } from "react";
import {
StyleSheet,
View,
Text,
Image,
TouchableOpacity,
useWindowDimensions,
Platform,
} from "react-native";
import { Controller } from "react-hook-form";
import * as ImagePicker from "expo-image-picker";
import { Ionicons } from "#expo/vector-icons";
//import dependencies
import { COLORS, SIZES, images } from "../constants";
const CustomImagePicker = ({ control, name, rules = {} }) => {
const { height } = useWindowDimensions();
const [hasGalleryPermission, setHasGalleryPermission] = useState("false");
const [profilePicture, setProfilePicture] = useState(name);
useEffect(() => {
async () => {
const galleryStatus =
await ImagePicker.requestMediaLibraryPermissionsAsync();
setHasGalleryPermission(galleryStatus.status === "granted");
};
}, []);
const pickImage = async () => {
let chosenImage = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
console.log(JSON.stringify(chosenImage));
if (!chosenImage.cancelled) {
setProfilePicture(chosenImage.uri);
}
};
if (hasGalleryPermission === false) {
return <Text>❌ No access to Internal Storage</Text>;
}
return (
<Controller
name={name}
control={control}
rules={rules}
render={({ field: { onChange, value }, fieldState: { error } }) => (
<>
<View
style={[
styles.container,
{ borderColor: error ? COLORS.red : COLORS.gray },
]}
>
<TouchableOpacity
style={styles.touchPicture}
onPress={() => pickImage()}
>
<Ionicons name="pencil-outline" size={24} color={COLORS.white} />
</TouchableOpacity>
<Image
onChange={onChange}
value={value}
source={{
uri: profilePicture ? profilePicture : images.defaultRounded,
}}
style={[
styles.logo,
styles.profileImage,
{ height: height * 0.19 },
]}
resizeMode={Platform.OS === "android" ? "contain" : "cover"}
/>
</View>
{error && (
<Text
style={{
color: COLORS.red,
alignSelf: "stretch",
fontSize: SIZES.body5,
padding: SIZES.padding - 22,
marginTop: 15,
marginHorizontal: SIZES.padding * 3,
}}
>
{error.message || "❌ Oops, something went wrong!"}
</Text>
)}
</>
)}
/>
);
};
export default CustomImagePicker;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
touchPicture: {
zIndex: 10,
marginBottom: -50,
marginLeft: 100,
resizeMode: "contain",
},
logo: {
width: Platform.OS == "android" ? 155 : 164,
maxWidth: 300,
maxHeight: 200,
},
profileImage: {
marginTop: SIZES.padding * 2,
borderRadius: 100,
},
});

NetInfo crash in android

I'm using NetInfo to get the connection but some devices android crash with that
LOG:
Error Tag: Mini App Bundle Message: null Stack: android.net.ConnectivityManager$TooManyRequestsException at android.net.ConnectivityManager.convertServiceException(ConnectivityManager.java:3687) at android.net.ConnectivityManager.sendRequestForNetwork(ConnectivityManager.java:3924) at android.net.ConnectivityManager.registerDefaultNetworkCallback(ConnectivityManager.java:4334) at android.net.ConnectivityManager.registerDefaultNetworkCallback(ConnectivityManager.java:4311) at com.reactnativecommunity.netinfo.NetworkCallbackConnectivityReceiver.register(NetworkCallbackConnectivityReceiver.java:42) at com.reactnativecommunity.netinfo.NetInfoModule.initialize(NetInfoModule.java:38) at com.facebook.react.bridge.ModuleHolder.doInitialize(ModuleHolder.java:222) at com.facebook.react.bridge.ModuleHolder.markInitializable(ModuleHolder..java:97) at com.facebook.react.bridge.NativeModuleRegistry.notifyJSInstanceInitialized(NativeModuleRegistry.java:102) at com.facebook.react.bridge.CatalystInstanceImpl$2.run(CatalystInstanceImpl.java:441) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:26) at android.os.Looper.loop(Looper.java:237) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:225) at java.lang.Thread.run(Thread.java:919)
In my code:
import MaxApiManager from '#common/MaxApiManager';
import { throttle } from 'lodash';
import React, { useEffect, useState } from 'react';
import { View, TouchableOpacity, Image, StyleSheet, Text } from 'react-native';
import ChatImages from 'src/screens/Chat/img';
import { mapNameWithLocalContact } from 'src/screens/Chat/utils/ChatUtils';
import { Avatar, Skeleton } from '#momo-platform/component-kits';
import NetInfo from '#react-native-community/netinfo';
export function HeaderConversation({
relationShip,
relationshipText,
friendInfo = {},
goToProfile = () => {},
}) {
const [isConnected, setIsConnected] = useState(true);
useEffect(() => {
NetInfo?.fetch()?.then(state => {
setIsConnected(state.isConnected);
});
}, []);
return (
<View style={styles.headerLeft}>
<TouchableOpacity
style={styles.buttonBack}
onPress={throttle(() => MaxApiManager.dismiss(), 1000, {
leading: true,
trailing: false,
})}
>
<Image source={ChatImages.ic_back} style={styles.icBack} />
</TouchableOpacity>
{relationShip || isConnected === false ? (
<TouchableOpacity onPress={goToProfile} style={styles.info}>
<Avatar
size="small"
name={friendInfo?.name}
source={{ uri: friendInfo?.avatar }}
/>
<View style={{ marginLeft: 12 }}>
<Text style={styles.txtRoomName}>
{mapNameWithLocalContact({
phone: friendInfo?.phone,
name: friendInfo?.name,
})}
</Text>
{relationShip && <Text>{relationshipText}</Text>}
</View>
</TouchableOpacity>
) : (
<View style={{ paddingTop: 12 }}>
<Skeleton.Custom
left={<Skeleton.Media size={35} />}
style={styles.skeletonItem}
>
<Skeleton.Line style={styles.width_1_9} />
<Skeleton.Line style={styles.width_1_10} />
</Skeleton.Custom>
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
headerLeft: {
paddingLeft: 8,
flexDirection: 'row',
alignItems: 'center',
},
buttonBack: { padding: 8, marginRight: 4 },
width_1_9: { width: 150, height: 16 },
width_1_10: { width: 100, height: 12, marginTop: -6 },
skeletonItem: {
marginVertical: 5,
},
info: {
flexDirection: 'row',
alignItems: 'center',
},
txtRoomName: { fontSize: 16, fontWeight: 'bold' },
icBack: { width: 24, height: 24 },
});
AnalyticsModule.js
const AnalyticModule = {
netInfoSub: null,
initialize(deviceInfo) {
UserProfile.getInstance().getData().then((profile) => {
Storage.getItem("", (ipAddress) => {
StorageCache.get("").then(location => {
if (!this.netInfoSub) {
this.netInfoSub = NetInfo.addEventListener((state) => {
let netInfo = getNetworkInfo(state);
this.TRACKING_NETWORK_INFO = JSON.stringify(netInfo);
})
}
})
})
})
},
}
More infor:
#react-native-community/netinfo: ^5.9.9
react-native: 0.61.5
The easiest way to obtain network status information in your functional component is by using useNetInfo hook
import {useNetInfo} from '#react-native-community/netinfo';
const YourComponent = () => {
const netInfo = useNetInfo();
return (
<View>
<Text>Type: {netInfo.type}</Text>
<Text>Is Connected? {netInfo.isConnected.toString()}</Text>
</View>
);
};
for more details on property object: netinfo Docs
We have an eventListener for NetInfo and the mistake is we don't remove this event and with android, NetworkCallbacks have a limit(Maybe is 100 enter link description here)

Results do not update after a change of state

I have a problem, when I do a search, I get the data from my API, the first time I do a search, everything is fine, all the data is displayed. However, when I do a second search immediately, nothing is updated.
I put in console.log, and I see that I'm getting this data back, yet the display is not updated.
import React, { Component } from "react";
import { SafeAreaView, StyleSheet } from "react-native";
import Search from "./Component/Search";
export default class App extends Component {
render() {
return (
<SafeAreaView style={styles.container}>
<Search />
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});
import React from "react";
import { View, TextInput, Button, FlatList, StyleSheet } from "react-native";
import LivreItem from "../Component/LivreItem";
class Search extends React.Component {
constructor(props) {
super(props);
this.inputValue = "";
this.state = { livres: [] };
}
searchBooks = async () => {
const key = "&key=XXXXXXXXXXXXXXXXXXXXXXX";
const url = "https://www.googleapis.com/books/v1/volumes?q=" + this.inputValue + key;
return fetch(url)
.then(response => response.json())
.catch(e => {
console.log("Une erreur s'est produite");
console.log(e);
});
};
getBooks = () => {
if (this.inputValue.length > 0) {
this.searchBooks()
.then(data => this.setState({ livres: data.items }))
.catch(reject => console.log(reject));
}
};
changeText = text => {
this.inputValue = text;
};
render() {
return (
<View style={styles.header_container}>
<View style={styles.sub_container}>
<TextInput
onChangeText={text => this.changeText(text)}
style={styles.input}
placeholder="Ex: Harry Potter"
/>
<Button
style={styles.button}
title="Rechercher"
onPress={() => this.getBooks()}
/>
</View>
<FlatList
style={styles.list}
data={this.state.livres}
keyExtractor={(item, index) => item + index}
renderItem={({ item }) => <LivreItem livre={item.volumeInfo} />}
/>
</View>
);
}
}
const styles = StyleSheet.create({
sub_container: {
justifyContent: "space-between",
flexDirection: "row",
marginTop: 30,
paddingRight: 10,
paddingLeft: 10
},
header_container: {
flex: 1,
flexDirection: "column",
padding: 10
},
input: {
borderRadius: 4,
borderWidth: 0.5,
borderColor: "#d6d7da",
width: 150,
paddingLeft: 5
},
button: {
borderRadius: 4
},
list: {
paddingLeft: 15,
paddingRight: 15
}
});
export default Search;
import React from "react";
import { View, StyleSheet, Image, Text } from "react-native";
class LivreItem extends React.Component {
constructor(props) {
super(props);
this.state = { livre: this.props.livre};
this.description =
this.state.livre.description === null || this.state.livre.description === undefined
? "Pas de description disponible"
: this.state.livre.description;
this.img = this.state.livre.imageLinks;
this.image =
this.img === undefined ||
this.img.smallThumbnail === undefined ||
this.img.smallThumbnail === null
? null
: this.state.livre.imageLinks.smallThumbnail;
}
render() {
return (
<View style={styles.content}>
<View>
<Image style={styles.image} source={{ uri: this.image }} />
<Image style={styles.image} source={this.image} />
</View>
<View style={styles.content_container}>
<Text style={styles.titre}>{this.state.livre.title}</Text>
<Text style={styles.description} numberOfLines={4}>
{this.description}
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
content: {
height: 125,
flexDirection: "row",
marginTop: 15
},
content_container: {
flexDirection: "column",
flexShrink: 1,
marginLeft: 10
},
image: {
width: 100,
height: 100
},
titre: {
fontWeight: "bold",
flexWrap: "wrap"
},
description: {
flexWrap: "wrap"
}
});
export default LivreItem;
Thanks.
Configure the prop extraData in Flatlist component ala:
<FlatList
extraData={this.state.livres}
/>
Pass a boolean value to the FlatList extraData.
<FlatList
extraData={this.state.refresh}
/>

React Native Animated lagging - Prevent Re-Rendering by function call

Context
Im want to get a countdown value which I then want to pass as prop to a separate component animating a progress bar. the data.json contains two type of elements:
text
question
the text will present the user an explanation. when the user pushes the button another text can be shown or a question. when it is a question the user will have certain amount of time for the answer. the progress bar will indicate the remaining time and shall only be shown when the current element is a question.
Problem
My problem is that the screen update for the current value is super slow and I can't see the reason. here is a simplified version of my code. the progress bar component will replace the <Text>{this.state.value}</Text> within the renderButtons function
Versions
I am running react native 0.60.
Update 1
I found out that when I trigger the countdown function by an onPress event, it works. but if I call it directly it doesn't. Why is that? How could I achieve it without the onPress
Update 2
the problem is that due to the animation the state value gets updated an by that a serenader gets triggered, which causes the function to be called each time, which is causing the lagging. this workaround seems to help but it feels ugly.
what would be a better approach to handle the re-rendering issue?
countdown = type => {
if (!this.state.countdownRunning) {
this.setState({ countdownRunning: true });
if (type === 'question') {
this.state.percent.addListener(({ value }) => this.setState({ value }));
Animated.timing(
// Animate value over time
this.state.percent, // The value to drive
{
toValue: 0, // Animate to final value of 1
duration: 25000,
easing: Easing.linear
}
).start(() => {
console.log('Animation DONE');
this.setState({ value: 100, percent: new Animated.Value(100) });
this.onPressNext();
this.setState({ countdownRunning: false });
}); // Start the animation
}
}
};
Lagging
import React, { Component } from 'react';
import { View, Text, StyleSheet, Animated, Easing } from 'react-native';
import { Button } from 'react-native-paper';
import Card from '../components/Card';
import data from '../data/data.json';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5
},
surface: {
padding: 20,
margin: 20,
borderRadius: 10,
alignItems: 'center',
justifyContent: 'center',
elevation: 12
}
});
class Home extends Component {
constructor(props) {
super(props);
this.countdown = this.countdown.bind(this);
this.state = {
i: 0,
questions: data,
value: 100,
percent: new Animated.Value(100)
};
}
onPressNext = () => {
const { i } = this.state;
if (i < 17) {
this.setState({ i: i + 1 });
} else {
this.setState({ i: 0 });
}
};
onPressBack = () => {
const { i } = this.state;
if (i > 0) {
this.setState({ i: i - 1 });
} else {
this.setState({ i: 17 });
}
};
countdown = type => {
if (type === 'question') {
this.state.percent.addListener(({ value }) => this.setState({ value }));
Animated.timing(
// Animate value over time
this.state.percent, // The value to drive
{
toValue: 0, // Animate to final value of 1
duration: 25000,
easing: Easing.linear
}
).start(); // Start the animation
}
};
renderButtons = type => {
if (type === 'question') {
this.countdown(type);
return (
<View>
<Text>{this.state.value}</Text>
</View>
);
}
return (
<View style={{ flexDirection: 'row' }}>
<Button mode="text" onPress={() => this.onPressBack()}>
Zurück
</Button>
<Button mode="contained" onPress={() => this.onPressNext(type)}>
Weiter
</Button>
</View>
);
};
render() {
const { i, questions } = this.state;
const { type, header, content } = questions.data[i.toString()];
return (
<View style={styles.container}>
<View style={{ flex: 2, justifyContent: 'flex-end' }}>
<Card>
<Text>{header}</Text>
<Text>{content}</Text>
</Card>
</View>
<View style={{ flex: 2 }}>{this.renderButtons(type)}</View>
</View>
);
}
}
export default Home;
No Lagging
import React, { Component } from 'react';
import { View, Text, StyleSheet, Animated, Easing } from 'react-native';
import { Button } from 'react-native-paper';
import Card from '../components/Card';
import data from '../data/data.json';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5
},
surface: {
padding: 20,
margin: 20,
borderRadius: 10,
alignItems: 'center',
justifyContent: 'center',
elevation: 12
}
});
class Home extends Component {
constructor(props) {
super(props);
this.countdown = this.countdown.bind(this);
this.state = {
i: 0,
questions: data,
value: 100,
percent: new Animated.Value(100)
};
}
onPressNext = () => {
const { i } = this.state;
if (i < 17) {
this.setState({ i: i + 1 });
} else {
this.setState({ i: 0 });
}
};
onPressBack = () => {
const { i } = this.state;
if (i > 0) {
this.setState({ i: i - 1 });
} else {
this.setState({ i: 17 });
}
};
countdown = type => {
if (type === 'question') {
this.state.percent.addListener(({ value }) => this.setState({ value }));
Animated.timing(
// Animate value over time
this.state.percent, // The value to drive
{
toValue: 0, // Animate to final value of 1
duration: 25000,
easing: Easing.linear
}
).start(); // Start the animation
}
};
renderButtons = type => {
if (type === 'question') {
return (
<View>
<Text>{this.state.value}</Text>
<Button mode="contained" onPress={() => this.countdown(type)}>
Weiter
</Button>
</View>
);
}
return (
<View style={{ flexDirection: 'row' }}>
<Button mode="text" onPress={() => this.onPressBack()}>
Zurück
</Button>
<Button mode="contained" onPress={() => this.onPressNext(type)}>
Weiter
</Button>
</View>
);
};
render() {
const { i, questions } = this.state;
const { type, header, content } = questions.data[i.toString()];
return (
<View style={styles.container}>
<View style={{ flex: 2, justifyContent: 'flex-end' }}>
<Card>
<Text>{header}</Text>
<Text>{content}</Text>
</Card>
</View>
<View style={{ flex: 2 }}>{this.renderButtons(type)}</View>
</View>
);
}
}
export default Home;