ReactNative checkbox inside flatlist - react-native

I have checkboxes inside Flatlist like this
constructor(props) {
super(props);
this.state = {
checked: false
}
}
breakfastData = ({item}) => {
return(
<ListItem style={{flex:1 , flexDirection: 'row' , justifyContent:'space-between'}} >
<Text>{item}</Text>
<CheckBox
checked={this.state.checked}
onPress={() => this.setState({checked: !this.state.checked})} style={{ alignSelf: 'flex-end'}} color="#FC7B04" />
</ListItem>
)
}
render(){
return(
<View>
<FlatList
data={this.state.breakfast}
renderItem={ this.breakfastData }
keyExtractor={(item, index) => index}
/>
</View>
)
}
here is a screenshot from the app but the checkboxes don't work when i click any of the check boxes
i just want the user feel that the check box is checked

You must set checked for each item.
constructor(props) {
super(props);
this.state = {
breakfast:[
{id:1,checked:false},
{id:2,checked:false}
]
}
}
onItemPress = (item, index) => {
var tempItem = item
tempItem.checked = !item.checked
const tempArr = [...this.state.breakfast]
tempArr[index] = tempItem
this.setState({
breakfast: tempArr
})
}
breakfastData = ({item}) => {
return(
<ListItem style={{flex:1 , flexDirection: 'row' , justifyContent:'space-between'}} >
<Text>{item}</Text>
<TouchableOpacity onPress={() => {this.onItemPress(item,index)}}>
<CheckBox checked={item.checked} style={{ alignSelf: 'flex-end'}} color="#FC7B04" />
</TouchableOpacity>
</ListItem>
)
}
render(){
return(
<View>
<FlatList
data={this.state.breakfast}
renderItem={ this.breakfastData }
extraData ={ this.state}
keyExtractor={(item, index) => index}
/>
</View>
)
}

Related

How to add to export default class extends Component - ({navigation})?

I have a problem with adding the {navigation} parameter to the export default class extends Component, I need it for the FlatList
How can I add it here?
export default class ENews extends Component {
render() {
return (
<View style={styles.main}>
<StatusBar style='auto'/>
<FlatList
data={this.state.data}
onRefresh={() => this.onRefresh()}
refreshing={this.state.isFetching}
initialNumToRender={4}
contentContainerStyle={{ alignSelf: 'stretch' }}
keyExtractor={({ id }, index) => id}
renderItem={({item}) => (
<TouchableOpacity onPress={() => navigation.navigate('FullNews', item)}>
</TouchableOpacity>
)} />
</View>
);
}}
You need to get navigation from props you can either deconstruct this.props and get navigation like this
const { navigation } = this.props;
or you can directly use it like this
this.props.navigation.navigate('FullNews', item)
But I suggest using deconstructing props
export default class ENews extends Component {
render() {
const { navigation } = this.props;
return (
<View style={styles.main}>
<StatusBar style='auto'/>
<FlatList
data={this.state.data}
onRefresh={() => this.onRefresh()}
refreshing={this.state.isFetching}
initialNumToRender={4}
contentContainerStyle={{ alignSelf: 'stretch' }}
keyExtractor={({ id }, index) => id}
renderItem={({item}) => (
<TouchableOpacity onPress={() => navigation.navigate('FullNews', item)}>
</TouchableOpacity>
)} />
</View>
);
}}
Parent class
export default class ComponentName extends Component {
render() {
return (
<>
...other component
<ENews {...this.props}></ENews>
</>
);
}
}
you Component
export default class ENews extends Component {
render() {
const {navigation} = this.props; // here you destructuring you props
return (
<View style={styles.main}>
<StatusBar style="auto" />
<FlatList
data={this.state.data}
onRefresh={() => this.onRefresh()}
refreshing={this.state.isFetching}
initialNumToRender={4}
contentContainerStyle={{alignSelf: 'stretch'}}
keyExtractor={({id}, index) => id}
renderItem={({item}) => (
<TouchableOpacity
onPress={() => navigation.navigate('FullNews', item)}
/>
)}
/>
</View>
);
}
}
change as above in your code

Reset all the state of child and parent class by a button in the parent class - React native

I've a parent class with its child classes. When the child class is clicked, its id is passed to parent class and the bg color of the child is changed.
I want to reset all the state and bgColor of child class by clicking the button in the parent class. How can I achieve it?
Thanks in advance.
Parent class:
getSelectedChilds = (id) => {
const items = this.state.selectedIds.filter(item => item.id !== id);
this.setState({
selectedIds: [...items]
});
}
render() {
return(
<View>
<FlatList
data={data}
renderItem={({ item, index }) => <Child getSelectedChilds={this.getSelectedChilds} item={item} />}
/>
<Button
onPress={() => {
// how to reset all the states (parent & child) by clicking this button?
}}
title="Submit"
/>
</View>
)
}
Child class
export default class Child extends React.Component {
constructor(props) {
super(props);
this.state = {
selectChild: false
}
}
selectedChild = (id) => {
this.setState({
selectChild: !this.state.selectChild
});
this.props.getSelectedChilds(id);
}
render() {
const { item } = this.props;
return (
<View style={{ flex: 1 }}>
<TouchableOpacity
activeOpacity={0.8}
onPress={() => {
this.selectedChild(item.id);
}}>
<View style={[{ backgroundColor: this.state.selectChild ? 'red' : 'transparent' }]}>
<View style={{ flexDirection: 'row' }}>
<Text>{item.name}</Text>
</View>
</View>
</TouchableOpacity>
</View>
)
}
}
What I did so far:
I used refs to access the function of child. It works but the state of child classes can not be changed, and hence bgColor is red all the time.
Parent class
constructor(props) {
super(props);
this.child = React.createRef();
}
resetAll = () => {
this.setState({
selectedIds: [...items]
});
this.child.current.reset(); // this is not working properly
}
render() {
return(
<View>
<FlatList
data={data}
renderItem={({ item, index }) => <Child ref={this.child} getSelectedChilds={this.getSelectedChilds} item={item} />}
/>
<Button
onPress={() => {
this.resetAll()
}}
title="Submit"
/>
</View>
)
}
Child class
reset = () => {
alert('abc'); //the alert works but the line after this doesn't work and the bg color is unchanged.
this.setState({
selectChild: false
});
}
render() {
const { item } = this.props;
return (
<View style={{ flex: 1 }}>
<TouchableOpacity
activeOpacity={0.8}
onPress={() => {
this.selectedChild(item.id);
}}>
<View style={[{ backgroundColor: this.state.selectChild ? 'red' : 'transparent' }]}>
<View style={{ flexDirection: 'row' }}>
<Text>{item.name}</Text>
</View>
</View>
</TouchableOpacity>
</View>
)
}
You could just pass a boolean in your child to tell him to reset its own state. Something like :
const resetAll = () => {
this.setState({
// ... whatever you want,
resetChild: true
});
}
And then
<Child shouldReset={this.state.resetChild} />
By cliking on your button, you will trigger a re-render in your child and this child will handle its own state.

Retrieve a list of products and display them

I am trying to retrieve a list of products from my API. For the moment, nothing is displayed, the page is currently empty except for my back button and I do not understand why. The two functions I use are functional since both console.log works. Could you help me ?
Everything looks fine, the console.log work in the terminal but I can't display anything in the app.
I tried this snack : https://snack.expo.io/O4oPj8-Qz
const Item = ({ item, onPress, style }) => (
<TouchableOpacity onPress={onPress} style={[styles.productItem, style]}>
<Text style={[styles.h4, {textAlign: "left"}]}>
{item.name}
</Text>
</TouchableOpacity>
);
export default class Products extends Component {
constructor(props) {
super(props);
this.state = {
selectedId: '',
setSelectedId: '',
listData: '',
currentPage: 1,
loadMoreVisible: true,
loadMoreVisibleAtEnd: false,
displayArray: null
}
};
initListData = async () => {
let list = await getProducts(1);
console.log(list)
if (list) {
this.setState({
displayArray: list,
loadMoreVisible: (list.length >= 15 ? true : false),
currentPage: 2
});
}
};
setNewData = async (page) => {
let list = await getProducts(parseInt(page));
if (list) {
this.setState({
displayArray: this.state.displayArray.concat(list),
loadMoreVisible: (list.length >= 15 ? true : false),
loadMoreVisibleAtEnd: false,
currentPage: parseInt(page)+1
});
}
};
loadMore() {
this.setNewData(this.state.currentPage);
}
displayBtnLoadMore() {
this.setState({
loadMoreVisibleAtEnd: true
});
}
async UNSAFE_componentWillMount() {
this.initListData();
}
render() {
return (
<View>
{this.state.displayArray !== null && this.state.displayArray.length > 0 ? (
<View style={{ flex: 1, marginBottom: 100 }}>
<SafeAreaView style={styles.container}>
<FlatList
data={this.state.displayArray}
extraData={this.selectedId}
onEndReached={() => this.displayBtnLoadMore()}
renderItem={({item})=>
<View style={{flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center'}}>
<Item
item={item}
onPress={() => this.props.navigation.navigate('ProductDetails', {productId: parseInt(item.id)})}
/>
</View>
}
keyExtractor={item => "product-" + item.id.toString()}
style={{width:"90%"}}
/>
{this.state.loadMoreVisible === true && this.state.loadMoreVisibleAtEnd === true ? (
<Button title=" + " onPress={()=>{this.loadMore()}}></Button>
) : null
}
<View style={styles.container}>
<Text>{"\n"}</Text>
<TouchableOpacity
style={styles.touchable2}
onPress={() => this.props.navigation.goBack()}
>
<View style={styles.view2}>
<Text style={styles.textimg2}>
back
</Text>
</View>
</TouchableOpacity>
</View>
<Text>{"\n\n"}</Text>
</SafeAreaView>
</View>
) : (
<View style={styles.container}>
<Text>{"\n\n" + (this.state.displayArray === null ? i18n.t("products.searching") : i18n.t("products.nodata")) + "\n\n\n"}</Text>
<TouchableOpacity
style={styles.touchable2}
onPress={() => this.props.navigation.goBack()}
>
<View style={styles.view2}>
<Text style={styles.textimg2}>
Back
</Text>
</View>
</TouchableOpacity>
</View>
)}
</View>
);
};
}
You were not adding flex: 1 and also not calling the API in the right way, here is the snack with the solution.
Link to snack
Thanks to your answers and help. I got it work this way : Thank you so much for yout time and help, really, sincerely.
import React, { Component } from "react";
import { FlatList, SafeAreaView, Button, Text, View, TouchableOpacity } from 'react-native';
import { getProducts } from '../../../src/common/Preferences';
import styles from '../../../assets/styles';
import i18n from '../../../src/i18n';
const Item = ({ item, onPress, style }) => (
<TouchableOpacity onPress={onPress} style={[styles.productItem, style]}>
<Text style={[styles.h4, {textAlign: "left"}]}>
{item.name}
</Text>
</TouchableOpacity>
);
export default class Products extends Component {
constructor(props) {
super(props);
this.state = {
selectedId: '',
setSelectedId: '',
listData: '',
currentPage: 1,
loadMoreVisible: true,
loadMoreVisibleAtEnd: false,
displayArray: []
}
};
initListData = async () => {
let list = await getProducts(1);
if (list) {
this.setState({
displayArray: list,
loadMoreVisible: (list.length >= 15 ? true : false),
currentPage: 2
});
console.log(this.state.displayArray, 'dans initListData')
}
};
setNewData = async (page) => {
let list = await getProducts(parseInt(page));
if (list) {
this.setState({
displayArray: this.state.displayArray.concat(list),
loadMoreVisible: (list.length >= 15 ? true : false),
loadMoreVisibleAtEnd: false,
currentPage: parseInt(page)+1
});
}
};
loadMore() {
this.setNewData(this.state.currentPage);
}
displayBtnLoadMore() {
this.setState({
loadMoreVisibleAtEnd: true
});
}
async UNSAFE_componentWillMount() {
this.initListData();
console.log(this.state.displayArray, 'dans componentWillMount')
}
render() {
console.log('displayArray', this.state.displayArray)
return (
<View style={{flex: 1}}>
<Text>{"\n"}</Text>
<Text>{"\n"}</Text>
{this.state.displayArray !== null && this.state.displayArray.length > 0 ? (
<View style={{ flex: 1, marginBottom: 100 }}>
<SafeAreaView style={styles.container}>
<FlatList
data={this.state.displayArray}
//extraData={this.selectedId}
//onEndReached={() => this.displayBtnLoadMore()}
renderItem={({item})=>
<View style={{flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center'}}>
<Item
item={item}
onPress={() => this.props.navigation.navigate('ProductDetails', {productId: parseInt(item.id)})}
/>
</View>
}
keyExtractor={item => "product-" + item.id.toString()}
style={{width:"90%"}}
/>
{this.state.loadMoreVisible === true && this.state.loadMoreVisibleAtEnd === true ? (
<Button title=" + " onPress={()=>{this.loadMore()}}></Button>
) : null
}
<View style={styles.container}>
<Text>{"\n"}</Text>
<TouchableOpacity
style={styles.touchable2}
onPress={() => this.props.navigation.goBack()}
>
<View style={styles.container}>
<Button
color="#F78400"
title= 'Back'
onPress={() => this.props.navigation.goBack()}>BACK
</Button>
</View>
</TouchableOpacity>
</View>
<Text>{"\n\n"}</Text>
</SafeAreaView>
</View>
) : (
<View style={styles.container}>
<Text>{"\n\n" + (this.state.displayArray === null ? i18n.t("products.searching") : i18n.t("products.nodata")) + "\n\n\n"}</Text>
<Button
color="#F78400"
title= 'Back'
onPress={() => this.props.navigation.goBack()}>BACK
</Button>
</View>
)}
</View>
);
};
}

React Native FlatList Delete Item

I want to delete item from the FlatList. However, the solution from here is not working for my code. When I run my code, I am getting error such as 'index is not defined'.
I have yet to find other post regarding this issue so any help would be greatly appreciated.
Code snippet provided below:
export default class FrCreateScreen extends Component {
constructor(props) {
super(props);
this.state = {
//new timeSlots
timeSlots: [],
}
}
setEndTime = (event, appointmentEndTime, textEndTime) => {
appointmentEndTime = appointmentEndTime || this.state.appointmentEndTime;
textEndTime = textEndTime || moment(appointmentEndTime).format('h:mm a').toString();
this.setState({
showEndTime: Platform.OS === 'ios' ? true : false,
appointmentEndTime,
textEndTime,
timeSlots: [
...this.state.timeSlots,
{
apptdate: this.state.textAppointmentDate,
appttime: this.state.textAppointmentTime,
endTime: textEndTime,
}
],
});
}
deleteDateTime = (id) => {
const filteredData = this.state.timeSlots.filter(item => item.id !== id);
this.setState({ timeSlots: filteredData });
}
render() {
return (
<ScrollView>
...
<View>
<FlatList
data={this.state.timeSlots}
keyExtractor={({ id }, index) => index.toString()}
renderItem={({ item }) => {
return (
<View style={styles.containerList}>
...
<TouchableOpacity onPress={() => this.deleteDateTime(index)}>
<Feather name="trash" style={styles.icon} />
</TouchableOpacity>
</View>
</View>
);
}}
/>
</View>
</ScrollView>
)
};
};
Screenshot below:
I think you need to add index inside renderItem { item, index }
renderItem = {({ item, index }) => {
return (
<View style={styles.containerList}>
<TouchableOpacity onPress={() => this.deleteDateTime(index)}>
<Feather name="trash" style={styles.icon} />
</TouchableOpacity>
</View>
);
}}
While rendering in map function , it provides the index too, so try adding that.
renderItem={({ item,index }) => {
return (
<View style={styles.containerList}>
...
<TouchableOpacity onPress={() => this.deleteDateTime(index)}>
<Feather name="trash" style={styles.icon} />
</TouchableOpacity>
</View>
</View>
);
}}
Hope it helps
ok fixed. on touchable onPress, the argument should be 'item.index' instead of 'index'.
here's the correct code:
renderItem={({ item,index }) => {
return (
<View style={styles.containerList}>
...
<TouchableOpacity onPress={() => this.deleteDateTime(item.index)}>
<Feather name="trash" style={styles.icon} />
</TouchableOpacity>
</View>
</View>
);
}}

My flatlist not rendering without remote debugger

I am working on an app by react native.i have a problem. when my app has a debug mode work very well. but in without debug mode, my Flatlist is not rendering all items.
I'm using redux and redux-saga
class ComponentGroups extends React.Component {
constructor (props){
super(props)
this.state = {
listBanner: this.props.item.listBanner
}
}
eventOnscroll = () => {
console.log('load list banner pls what wrong with u')
}
checkRenderView = () => {
if(this.state.listBanner.length && this.state.listBanner.length === 1) {
return (
<ComponentBanner item={this.state.listBanner[0]} typeTab={1}/>
)
}
const dimensions = Dimensions.get('window');
const screenWidth = dimensions.width;
return (
<ScrollView onScroll={this.eventOnscroll}>
<FlatList
style={{
flex: 1,
width: screenWidth
}}
showsHorizontalScrollIndicator={false}
horizontal={true}
data={this.state.listBanner}
removeClippedSubviews={false}
keyExtractor={(item, index) => index.toString()}
renderItem={({item, index}) => <ComponentBanner item={item} index={index} typeTab={1}/>}
/>
</ScrollView>
)
}
render () {
return (
<View style={styles.bannerItem}>
{this.state.listBanner.length && this.checkRenderView()}
</View>
)
}
}
and my component item:
class ComponentBanner extends React.Component {
constructor (props) {
super(props)
}
handlePressDetailBanner = () => {
detailImageScreen({item: this.props.item, typeTab: 1})
}
showViewNewsViewed = () => {
if(this.props.item.hot && this.props.item.watched) {
return (
<View style={styles.viewsLiked}>
<View style={styles.iconLike}>
<Text style={styles.news}>Mới</Text>
</View>
<View style={styles.iconView}>
<Text style={styles.viewed}>Đã xem</Text>
</View>
</View>
)
}
if(this.props.item.hot && !this.props.item.watched) {
return (
<View style={styles.viewsLiked}>
<View style={styles.iconLike}>
<Text style={styles.news}>Mới</Text>
</View>
</View>
)
}
if(!this.props.item.hot && this.props.item.watched) {
return (
<View style={styles.viewsLiked}>
<View style={styles.iconView}>
<Text style={styles.viewed}>Đã xem</Text>
</View>
</View>
)
}
}
render () {
return (
<View style={styles.bannerItem}>
<TouchableOpacity onPress ={this.handlePressDetailBanner}>
<Image source={{uri: this.props.item.url}} style={styles.imageItem} borderRadius={5} resizeMode='stretch'/>
</TouchableOpacity>
<View style={styles.titleLikes}>
{this.showViewNewsViewed()}
<Text numberOfLines={2} ellipsizeMode='tail' style={styles.textTitle}>{this.props.item.name}</Text>
<View style={styles.viewsLiked}>
<View style={styles.iconLike}>
<Icon
name='thumb-up'
color={this.props.item.userLike? Colors.mpAdvertise : Colors.cloud}
size={Fonts.size.regular}
/>
<Text style={styles.accountLike}>{this.props.item.totalLike}</Text>
</View>
<View style={styles.iconView}>
<Icon
name='eye'
color={Colors.cloud}
size={Fonts.size.regular}
/>
<Text style={styles.accountLike}>{this.props.item.totalView}</Text>
</View>
</View>
</View>
</View>
)
}
}
export default ComponentBanner
and Render Item by :
render () {
return (
<TouchableOpacity onPress ={this.handlePressDetailBanner} style={styles.detailImage}>
<View style={styles.bannerItem}>
<View>
<Image source={{uri: this.props.item.url ? this.props.item.url : ''}} style={styles.imageItem} borderRadius={5} resizeMode='stretch'/>
</View>
<View style={styles.titleLikes}>
<Text numberOfLines={2} ellipsizeMode='tail' style={styles.textTitle}>{this.props.item.name ? this.props.item.name : ''}</Text>
<View style={styles.viewsLiked}>
<View style={styles.iconLike}>
<Icon
name='thumb-up'
color={this.props.item.userLike? Colors.mpAdvertise : Colors.cloud}
size={Fonts.size.regular}
/>
<Text style={styles.accountLike}>{this.props.item.totalLike ? this.props.item.totalLike : ''}</Text>
</View>
<View style={styles.iconView}>
<Icon
name='eye'
color={Colors.cloud}
size={Fonts.size.regular}
/>
<Text style={styles.accountLike}>{this.props.item.totalView ? this.props.item.totalView : ''}</Text>
</View>
</View>
</View>
</View>
</TouchableOpacity>
)
}
}
UPDATE: sorry for forgot main screen rendering ComponentGroups:
constructor (props) {
super(props)
this.state = {
params: {
SourceType: '',
GroupCode: '',
NumOfRec: 10,
PageNum: 1,
TypeId: '',
},
data:[],
groupBanner: '',
groupVideo: '',
mainBanner: '',
listBanners: [],
listVideos: [],
isSelected: 1,
fetching: false,
onEndReachedCalledDuringMomentum: true
}
Navigation.events().bindComponent(this)
this.props.getData()
}
static getDerivedStateFromProps(nextProps, prevState){
if (nextProps.fetching !== prevState.fetching){
return {
fetching: nextProps.fetching,
data: nextProps.data,
listBanners: nextProps.listBanners,
listVideos: nextProps.listVideos
}
}
else return null;
}
componentDidUpdate(prevProps, prevState) {
if (prevState.fetching !== this.state.fetching) {
}
}
componentDidAppear () {
Navigation.mergeOptions(this.props.componentId, {
sideMenu: {
left: {
enabled: true
}
}
})
}
showSideMenu () {
Navigation.mergeOptions(this.props.componentId, {
sideMenu: {
left: {
visible: true
}
}
})
}
navigationButtonPressed ({ buttonId }) {
this.showSideMenu()
}
handlePressScroll = () => {
const {params} = this.state
if(!this.state.fetching) {
this.setState({
params: {
...params,
PageNum: params.PageNum + 1,
}
}, () => {
const data = Object.keys(this.state.params).map(key => key + '=' + this.state.params[key]).join('&')
console.log(data)
if(this.state.isSelected === 2) {
this.props.getDataBanner(data)
}
if(this.state.isSelected === 3) {
this.props.getDataVideo(data)
}
})
} else {
return null
}
}
handlePressSwitchTab = (value) => {
this.setState({
isSelected: value,
params: {
SourceType: 1,
GroupCode: '',
NumOfRec: 10,
PageNum: 1,
TypeId: 0
},
listBanners: [],
listVideos: [],
},()=>{
if(value === 1) {
this.props.resetStateHome()
this.props.getData()
}
if(value === 2) {
this.props.resetStateHome()
const data = Object.keys(this.state.params).map(key => key + '=' + this.state.params[key]).join('&')
this.props.getDataBanner(data)
}
if(value === 3) {
this.props.resetStateHome()
const data = Object.keys(this.state.params).map(key => key + '=' + this.state.params[key]).join('&')
this.props.getDataVideo(data)
}
})
}
formatData = (data, numColumns) => {
const numberOfFullRows = Math.floor(data.length / numColumns)
let numberOfElementsLastRow = data.length - (numberOfFullRows * numColumns)
while (numberOfElementsLastRow !== numColumns && numberOfElementsLastRow !== 0) {
data.push({ key: `blank-${numberOfElementsLastRow}`, empty: true })
numberOfElementsLastRow++
}
return data
}
handlePressBanner = () => {
detailImageScreen({item: this.state.data.mainBanner})
}
eventScroll = () => {
console.log(45544)
}
renderViewByState = () => {
const numColumns = 2;
if(this.state.data && this.state.data.mainBanner && this.state.data.mainBanner.url) {
var url = this.state.data.mainBanner.url
}
if (this.state.isSelected === 1) {
if(this.state.fetching) {
return (
<View style={styles.styleLoadMore}>
<BallIndicator color={Colors.mpAdvertise} animationDuration={800} />
</View>
)
} else {
return (
<ScrollView onScroll={this.eventScroll} >
<View style={styles.centered}>
<TouchableOpacity onPress={this.handlePressBanner} style={styles.mainBannerImage}>
<Image source={{uri: url}} style={styles.logo} borderRadius={5} resizeMode='stretch'/>
</TouchableOpacity>
</View>
<View style={styles.containerContent}>
<View style={styles.sectionBanner} >
<View>
<Text style={styles.textBanner}>BANNERS</Text>
</View>
<View style={styles.listBanner}>
<FlatList
data={this.state.data.groupBanner}
keyExtractor={(item, index) => index.toString()}
renderItem={({item, index}) => <ComponentGroups item={item} index={index} />}
/>
</View>
</View>
<View style={styles.sectionBanner} >
<View>
<Text style={styles.textBanner}>VIDEOS</Text>
</View>
<View style={styles.listBanner}>
<ScrollView>
<FlatList
data={this.state.data.groupVideo}
keyExtractor={(item, index) => index.toString()}
renderItem={({item, index}) => <ComponentGroupsVideo item={item} index={index} />}
/>
</ScrollView>
</View>
</View>
</View>
</ScrollView>
)
}
}
if (this.state.isSelected === 2) {
if(this.state.fetching && this.state.params.PageNum < 2) {
return (
<View style={styles.styleLoadMore}>
<BallIndicator color={Colors.mpAdvertise} animationDuration={800} />
</View>
)
}
return (
<ScrollView onScroll={this.eventScroll}>
<View style={styles.listBanner}>
<FlatList
extraData={this.state}
ListFooterComponent = {this.renderFooter}
data={this.state.listBanners}
keyExtractor={(item, index) => index.toString()}
renderItem={({item, index}) => <BannerItem item={item} index={index}/>}
numColumns={countNumber}
onEndReached={this.handlePressScroll}
onEndReachedThreshold={0.05}
/>
</View>
</ScrollView>
)
}
if (this.state.isSelected === 3) {
if(this.state.fetching && this.state.params.PageNum < 2) {
return (
<View style={styles.styleLoadMore}>
<BallIndicator color={Colors.mpAdvertise} animationDuration={800} />
</View>
)
}
return (
<View style={styles.listBanner}>
<FlatList
extraData={this.state}
data={this.state.listVideos}
ListFooterComponent = {this.renderFooter}
keyExtractor={(item, index) => index.toString()}
renderItem={({item, index}) => <VideosItem item={item} index={index} typeTab={3}/>}
numColumns={countNumber}
onEndReached={this.handlePressScroll}
onEndReachedThreshold={0.01}
/>
</View>
)
}
}
renderFooter = () => {
if(this.state.fetching && this.props.isStopScroll && this.state.params.PageNum > 1) {
return (
<View style={styles.styleLoadMoreFooter}>
<BallIndicator color={Colors.mpAdvertise} animationDuration={800} />
</View>
)
}
return null
}
render () {
return (
<View style={styles.mainContainer} testID='launchScreen'>
<View style={styles.tabBarHome}>
<View style={[styles.itemTab, this.state.isSelected === 1 ? styles.itemTabActive : null]}>
<TouchableOpacity testID='loginScreenLoginButton' style={styles.loginButtonWrapper} onPress={() => this.handlePressSwitchTab(1)}>
<View style={styles.loginButton}>
<Text style={[styles.itemText, this.state.isSelected === 1 ? styles.itemTextActive : null]}>Nổi bật</Text>
</View>
</TouchableOpacity>
</View>
<View style={[styles.itemTab, this.state.isSelected === 2 ? styles.itemTabActive : null]}>
<TouchableOpacity testID='loginScreenLoginButton' style={styles.loginButtonWrapper} onPress={() => this.handlePressSwitchTab(2)}>
<View style={styles.loginButton}>
<Text style={[styles.itemText, this.state.isSelected === 2 ? styles.itemTextActive : null]}>Banner</Text>
</View>
</TouchableOpacity>
</View>
<View style={[styles.itemTab, this.state.isSelected === 3 ? styles.itemTabActive : null]}>
<TouchableOpacity testID='loginScreenLoginButton' style={styles.loginButtonWrapper} onPress={() => this.handlePressSwitchTab(3)}>
<View style={styles.loginButton}>
<Text style={[styles.itemText, this.state.isSelected === 3 ? styles.itemTextActive : null]}>Videos</Text>
</View>
</TouchableOpacity>
</View>
</View>
{this.renderViewByState()}
</View>
)
}
}
const mapStateToProps = (state) => {
return {
data: state.home.data,
listBanners: state.home.listBanners,
listVideos: state.home.listVideos,
fetching: state.home.fetching,
cart: state.login.dataCart,
isStopScroll: state.home.isStopScroll
}
}
const mapDispatchToProps = (dispatch) => {
return {
getData: () => dispatch(HomeActions.homeRequest()),
getDataBanner: (data) => dispatch(HomeActions.bannerRequest(data)),
getDataVideo: (data) => dispatch(HomeActions.videoRequest(data)),
resetStateHome: () => dispatch(HomeActions.resetStateHome())
}
}
export default connect(mapStateToProps, mapDispatchToProps)(HomeScreen)
The above code, render my item in flatlist .in debug mode working very good, but in release mode, it's not rendering at all
please try this
<FlatList
style={{
flex: 1,
width: screenWidth
}}
extraData={this.state} //<--- add this line
showsHorizontalScrollIndicator={false}
horizontal={true}
data={this.state.listBanner}
removeClippedSubviews={false}
keyExtractor={(item, index) => index.toString()}
renderItem={({item, index}) => <ComponentBanner item={item} index={index} typeTab={1}/>}
/>