FlatList onendreached triggers indefinitely - react-native

My FlatList triggers onendreached not as expected. onEndReached gets called again and again. I have read some suggestion to wrap flatlist in a view with flex:1 but I still doesn't work properly. Also removing the scrollView didn't work-
This didn't help https://github.com/GeekyAnts/NativeBase/issues/1736#issuecomment-401815949
<View style={baseStyles.body}>
<View style={{flexDirection:"row", backgroundColor:theme.button.tertiary}}>
<View style={{flex:1}}>
<SearchBar
onChangeText={(query) => this.setState({query})}
placeholder='Hier suchen...'
showLoading
/>
</View>
</View>
<View style={{flex:1}}>
<ScrollView style={{flex: 1, flexDirection:'column'}}>
<View style={{flex:1}}>
<FlatList
data={articlesData}
renderItem={renderFunction}
onEndReached={this._onEndReached}
onEndThreshold={0}
refreshing={this.state.isLoading}
onRefresh={this.onRefresh}
keyExtractor={item => item.slug}
/>
</View>
<View style={{marginBottom:10}}>
<Text style={{color:this.state.theme.text.primary,textAlign:"center",fontSize:16}}>Gefunden: {rowCount}</Text>
</View>
</ScrollView>
</View>
</View>

If it is loading or error you can try to use onEndReached={this.state.isLoading ? () => {} : this._onEndReached}; just let onEndReached do nothing when the state is loading. I've tried serveral way (debounce, await setState, using flag) but only this way work as expected.
<View style={baseStyles.body}>
<View style={{flexDirection:"row", backgroundColor:theme.button.tertiary}}>
<View style={{flex:1}}>
<SearchBar
onChangeText={(query) => this.setState({query})}
placeholder='Hier suchen...'
showLoading
/>
</View>
</View>
<View style={{flex:1}}>
<ScrollView style={{flex: 1, flexDirection:'column'}}>
<View style={{flex:1}}>
<FlatList
data={articlesData}
renderItem={renderFunction}
onEndReached={this.state.isLoading ? () => {} : this._onEndReached}
onEndThreshold={0}
refreshing={this.state.isLoading}
onRefresh={this.onRefresh}
keyExtractor={item => item.slug}
/>
</View>
<View style={{marginBottom:10}}>
<Text style={{color:this.state.theme.text.primary,textAlign:"center",fontSize:16}}>Gefunden: {rowCount}</Text>
</View>
</ScrollView>
</View>
</View>

Related

Images are not showing after builds in react native

So whenever i try put any images in flatlist or data.map to display the images its not working , it works fine with emulator but when i make build it doesnt show images
'the code is :
{this.state.symptoms_second.map((row, index) => (
<View>
<TouchableOpacity
activeOpacity={1}
onPress={() => this.symptoms_doctor_list(row.id)}>
<View style={styles.home_style26}>
<Image
style={styles.home_style27}
source={{uri: img_url + row.service_image}}
/>
</View>
</TouchableOpacity>
{/* </Card> */}
<Text style={styles.home_style28}>{row.service_name}</Text>
</View>
))}
flatlist code
<FlatList
horizontal={true}
data={this.state.symptoms_first}
showsHorizontalScrollIndicator={false}
renderItem={({item, index}) => (
<View>
<TouchableOpacity
activeOpacity={1}
style={{margin: 2}}
onPress={() =>
this.symptoms_doctor_list(item.name)
}>
<View style={styles.home_style20}>
<Image
style={styles.home_style21}
source={{uri: item.image}}
/>
</View>
</TouchableOpacity>
<Text style={styles.home_style22}>{item.name}</Text>
</View>
)}
keyExtractor={item => item.id}
/>
it does load the api content of text but not images and images do have style property for height and width... also images are not loading in general case when the image is passed through uri

Horizontal Scroll View not working in react native

horizontal scroll view is not working in my react native app. Here is my code below:
return (
<View style={styles.container}>
{loading ? (
<ActivityIndicator size="large" color={COLORS.blue} />
) : (
kioskData.map((item, index) => {
return (
<View key={index} style={styles.container1}>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}>
<>
<Text>{item.product_price}</Text>
<Text>{item.product_name}</Text>
</>
</ScrollView>
</View>
);
})
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
color: COLORS.black,
backgroundColor: COLORS.white,
},
container1: {
flex: 1,
},
});
Am not sure where I am going wrong. Thanks for your help in advance.
The issue is with the usage of ScrollView in the wrong area which is inside of the loop (map).
Instead of creating an independent ScrollView for each item in the list, We can wrap the List Text elements within one single ScrollView Component.
let's update the ScrollView component tree to fix up the issue,
<View style={styles.container}>
{loading ? (
<ActivityIndicator size="large" color={COLORS.blue} />
) : (
<View style={styles.container1}>
<ScrollView horizontal showsHorizontalScrollIndicator={false}>
{kioskData.map((item, index) => {
return (
<Fragment key={index}>
<Text>{item.product_price}</Text>
<Text>{item.product_name}</Text>
</Fragment>
);
})}
</ScrollView>
</View>
)}
</View>
Additional Tip:
If the list is quite large and has some performance concerns you can go for FlatList component for the same UI implementation.
const renderItem = ({item}) => (
<>
<Text>{item.product_price}</Text>
<Text>{item.product_name}</Text>
</>
);
return (
<View style={styles.container}>
{loading ? (
<ActivityIndicator size="large" color={COLORS.blue} />
) : (
<View style={styles.container1}>
<FlatList
data={kioskData}
horizontal
showsHorizontalScrollIndicator={false}
renderItem={renderItem}
keyExtractor={item => item.id}
/>
</View>
)}
</View>
);
<View style={{flexDirection: 'row'}}>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}>
<Text>asdasdasd</Text>
<Text>asdasdasd</Text>
<Text>asdasdasd</Text>
<Text>asdasdasd</Text>
<Text>asdasdasd</Text>
<Text>asdasdasd</Text>
<Text>asdasdasd</Text>
<Text>asdasdasd</Text>
<Text>asdasdasd</Text>
<Text>asdasdasd</Text>
</ScrollView>
</View>

React Native scrollview not working in android

I am using a ScrollView in my react-native app. The App work fine in my iOS simulator but when I test in my Android emulator the ScrollView does not work.
Here is what my React Components returns
<SafeAreaView style={styles.container}>
<Image
source={require('../assets/images/logo.png')}
style={styles.logo}
/>
<Androw style={styles.shadow}>
<Text style={[styles.logoText, styles.shadow]}>{NAME}</Text>
</Androw>
<Text style={styles.title}>Welcome Back</Text>
<View style={styles.subContainer}>
<ScrollView showsVerticalScrollIndicator={false}>
<Text style={styles.loginText}>Login</Text>
<View style={styles.textBox}>
<Input
// icon="email"
placeholder="Email or username"
autoCapitalize="none"
autoCompleteType="email"
value={this.state.email}
onChangeText={(txt) => this.setState({email: txt})}
/>
<View style={styles.textInput}>
<Input
// icon="pass"
placeholder="Password"
autoCapitalize="none"
autoCompleteType="password"
value={this.state.password}
onChangeText={(txt) => this.setState({password: txt})}
/>
</View>
</View>
<TouchableOpacity>
<Text style={styles.forgotPasswordText}>
Don't remember your password?
</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => console.log(this.state)}>
<Button text="Login" />
</TouchableOpacity>
<Text style={styles.noAccountText}>
Don't have an account?{' '}
<Text style={styles.signupText}>Signup</Text>
</Text>
</ScrollView>
</View>
</SafeAreaView>
you should wrap your child views inside like
<ScrollView>
<View>
.....
</View>
</ScrollView>
Top <View> must have style flex:1. Alternate try doing like this
<ScrollView contentContainerStyle={{ flexGrow: 1 }}>
...
</ScrollView>
SafeAreaView that does not require flex: 1. Also try removing it from ScrollView too if it is there.
This is what i assume. Would be more helpful if you share your stylesheet also.

ScrollView isn't appering

I was messing with my code and suddenly the ScrollView decided to disappear.
For some reason, the ScrollView works "properly" inside the Character bar View
but when it is inside of the base View it just isn't there.
export default class CharacterSheet extends Component {
render() {
return (
<View style={{flex:1}}>
<View style={styles.TopBar}>
<View>
</View>
</View>
<View style={{flex:1}}>
<View style={styles.CharacterBar}>
</View><View style={styles.Base}>
<ScrollView style={styles.scroll}>
<View style={{paddingTop:72}}>
<View style={styles.spellsBox}>
<Text>Hi</Text>
</View>
<View style={styles.spellsBox}>
<Text>e</Text>
</View>
<View style={styles.spellsBox}>
<Text>a</Text>
</View>
<View style={styles.spellsBox}>
<Text>Hi</Text>
</View>
<View style={styles.spellsBox}>
<Text>Hi</Text>
</View>
<View style={styles.spellsBox}>
<Text>Hi</Text>
</View>
</View>
</ScrollView>
</View>
<Animated.View style={styles.CharacterStatsBar}>
</Animated.View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
CharacterBar:{
height:72,
backgroundColor:'#242527'
},
CharacterStatsBar:{
position:'absolute',
top: 72,
left: 0,
right: 0,
height:72,
backgroundColor:'#404040',
borderBottomWidth:2,
borderBottomColor:'#c53131',
borderTopWidth:1,
borderTopColor:'#838383',
},
Base:{
flex:1,
backgroundColor:'#fff',
},
scroll:{
width:'95%',
alignSelf:'center'
},
spellsBox:{
height:80,
marginTop:10,
paddingVertical:10,
borderColor:'#777',
borderWidth:3,
justifyContent:'center'
}
});
I'm trying to run this in an android device
this problem is pretty annoying and is stressing me a little bit.
Try Like this , don't wrap ScrollView inside multiple Views.
<View style={{flex:1}}>
<View style={styles.TopBar}>
<View>
</View>
</View>
<ScrollView style={styles.scroll}>
<View style={{paddingTop:72}}>
<View style={styles.spellsBox}>
<Text>Hi</Text>
</View>
<View style={styles.spellsBox}>
<Text>e</Text>
</View>
<View style={styles.spellsBox}>
<Text>a</Text>
</View>
<View style={styles.spellsBox}>
<Text>Hi</Text>
</View>
<View style={styles.spellsBox}>
<Text>Hi</Text>
</View>
<View style={styles.spellsBox}>
<Text>byee</Text>
</View>
</View>
</ScrollView>
</View>

FlatList in React Native does not render when only one item

I'm trying to render item using FlatList in React Native project. So my problem is when I render 2 items I can see the result but when there is only on item in my data array they is no result on my screen any idea why this is happening?
const data = this.getListPrise()
<View>
<View style={styles.allPAge} >
<View style={styles.centerText}>
<Text style={styles.text}>Voici vos differentes capture </Text>
</View>
<View style={{flex: 1}}>
<FlatList
data={data}
renderItem={({ item }) =>
<TouchableOpacity style={styles.container} onPress={() => this.modifCapture(item)}>
<Image style={styles.mark} source={avatar}></Image>
<View style={styles.displayUnder} >
<Text style={styles.textCapture}><Text style={styles.textCaptureTitle}>Espèce </Text>: {item.espece}</Text>
<Text style={styles.textCapture}><Text style={styles.textCaptureTitle}>Date </Text>:
{moment(item.date).format(" D MMMM YYYY, h:mm:ss")}</Text>
<Text style={styles.textCapture}><Text style={styles.textCaptureTitle}>Poids </Text> : {item.poids} kg</Text>
</View>
</TouchableOpacity>
}
style={{ flex: 1, flexDirection: 'column', padding: 1 }}
/>
</View>
<ActionButton buttonColor="rgba(231,76,60,1)" onPress={() => this.Addfish()}></ActionButton>
<View>
</View>
</View>
</View>