renderItemTop ({item}) {
return (
<TouchableOpacity
onPress={()=>this.props.navigation.navigate('DetailUpMovie')}
>
<Card
containerStyle={stylesTop.ImageCardStyle}
image={{ uri: `${BASE_IMAGE_URL}original${item.poster_path}` }}
imageStyle={{ width: CardWidthTop, height: CardHeightTop }}
imageProps={{ borderRadius: 15, resizeMode:'stretch' }}
/>
</TouchableOpacity>
);
}
render () {
const { data } = this.state;
return (
<View style={stylesTop.ViewTopstyle}>
<View style={stylesTop.InnerViewstyle}>
<Text style={stylesTop.TitleTopStyle}>Top Rated Movie</Text>
<Button
buttonStyle={stylesTop.btnStyle}
title='More'
titleStyle={stylesTop.btnTitleStyle}
type='clear'
/>
</View>
<FlatList
data={data}
renderItem={this.renderItemTop}
keyExtractor={item => item.id}
showsHorizontalScrollIndicator={false}
horizontal={true}
marginTop={5}
marginLeft={10}
marginRight={10}
marginBottom={5}
/>
</View>
);
}
this is where i want navigate to other screen in renderitem fuction but when i press on the item to navigate o got error tis.props.navigation.navigate undefined is not an object in another class i make all stack and bottomnavigator
please help
You can pass the navigation prop to your custom component via something like this:
You just need to pass the navigation prop to function to access the navigation action
const MyComponent = props => {
const { item, navigation } = props
return (
<TouchableOpacity
onPress={() => navigation.navigate('DetailUpMovie')}
>
<Card
containerStyle={stylesTop.ImageCardStyle}
image={{ uri: `${BASE_IMAGE_URL}original${item.poster_path}` }}
imageStyle={{ width: CardWidthTop, height: CardHeightTop }}
imageProps={{ borderRadius: 15, resizeMode:'stretch' }}
/>
</TouchableOpacity>
);
}
...
<FlatList
data={data}
renderItem={({item}) => (
<MyComponent item={item} navigation={this.props.navigation} />
)}
keyExtractor={item => item.id}
showsHorizontalScrollIndicator={false}
horizontal={true}
marginTop={5}
marginLeft={10}
marginRight={10}
marginBottom={5}
/>
Related
Good evening everyone .
I have created the following Flatlist :
<View style={{ flex: 1 }}>
<FlatList
ListFooterComponent={
loadMore && page < pageMax ? (
<ActivityIndicator color={Colors.grey40} />
) : (
<View />
)
}
ListFooterComponentStyle={{ height: 200 }}
contentContainerStyle={styles.listExercise}
keyExtractor={() => uuid.v4()}
data={exercises}
renderItem={renderItemRE}
removeClippedSubviews={true}
onEndReached={() => {
setOnEndReachedCalledDuringMomentum(true);
}}
onMomentumScrollEnd={loadMoreFc}
onEndReachedThreshold={0.3}
ListEmptyComponent={
<Text contentW B18>
Nessuna Esercizio presente
</Text>
}
/>
</View>
This is renderItem function
const renderItemRE = ({ item }) => {
return (
<RenderItemRE
selectables={route.params?.selectables}
item={item}
navigation={navigation}
/>
);
};
And finally this is my component RenderItemRE
const RenderItemRE = ({ item, navigation, selectables }) => {
return (
<View style={styles.globalContainer}>
<TouchableOpacity
style={styles.touchable}
onPress={() => {
navigation.navigate(Routes.InfoEsercizio, {
id_ex: item.id,
nomeEx: item.nome,
});
}}
>
<View style={styles.container}>
<Image
indicator={Progress}
style={styles.img}
source={{
uri: item.galleria
? item.galleria[0]
? item.galleria[0]
: "logo"
: ApiConstants.DEFAULT_IMAGE,
}}
/>
<Text
style={[
customtext(DYNAMIC_FONTS_SIZE.FONT_SIZE_BIG).regular,
styles.nameStyle,
]}
>
{item.nome}
</Text>
</View>
</TouchableOpacity>
</View>
);
};
function arePropsEqual(prevProps, nextProps) {
return nextProps.item.id === prevProps.item.id;
}
export default memo(RenderItemRE, arePropsEqual);
This is a correct way to use memo in react native ? Or am I doing something wrong? I'm noticing that when the list gets bigger the rendering slows down. I was wondering what was the correct way to optimize. Thank you in advance
Categories.js
<FlatList
data={categorylist}
keyExtractor={(item, index) => item.tc_id}
vertical
numColumns={4}
renderItem={({ item, index }) => {
return (
<Pressable onPress={subcat}>
<View style={styles.catLayout}>
<View style={styles.imageLayout}>
<Image source={{ uri: item.image }} style={styles.image}
/>
</View>
<Text style={styles.productname}>
{item.name != null ? item.name : ""}
</Text>
<Text>{item.uuid}</Text>
</View>
</Pressable>
);
}}
/>
I am getting data from categorylist array , i am displaying a bunch of items of categories here , i want show different sub-categories depending on each items "uuid".
I want to send the {item.uuid} to subcategories screen and reuse it in there.
As you are displaying the data on the view using flatlist you have onPress after clicking on any item you just need to send the data
Screen (A)
<FlatList
data={categorylist}
keyExtractor={(item, index) => item.tc_id}
vertical
numColumns={4}
renderItem={({ item, index }) => {
return (
<Pressable onPress={() => navigation.navigate('subcategories',{uuid:item.uuid}) }>
<View style={styles.catLayout}>
<View style={styles.imageLayout}>
<Image source={{ uri: item.image }} style={styles.image}
/>
</View>
<Text style={styles.productname}>
{item.name != null ? item.name : ""}
</Text>
<Text>{item.uuid}</Text>
</View>
</Pressable>
);
}}
/>
Screen (B)
you can get it like this
class component
constructor(props) {
super(props);
this.state = {
uuID: this.props.navigation.state.params.uuid,
}
}
functional component
const Subcategory = (props) => {
const [uUID, setuUID] = useState(props.route.params.uuid);
const Item = ({ item }) => (
<TouchableOpacity
onPress={() => this.navigation.navigate(
"carProfile",
{item: item}
)}>
<View style={styles.item}>
<Text style={styles.title}>{item.name}</Text>
<Text style={styles.details}>{item.details}</Text>
</View>
</TouchableOpacity>
);
const List = (props,navigation) => {
const renderItem = ({ item }) => {
if (props.searchPhrase === "") {
return <Item item={item} />;
}
if (item.name.toUpperCase().includes(props.searchPhrase.toUpperCase().trim().replace(/\s/g, ""))) {
return <Item item={item} />;
}
if (item.details.toUpperCase().includes(props.searchPhrase.toUpperCase().trim().replace(/\s/g, ""))) {
return <Item item={item} />;
}
};
return (
<SafeAreaView style={styles.list__container}>
<View
onStartShouldSetResponder={() => {
props.setClicked(false);
}}
>
<FlatList
data={props.data}
renderItem={renderItem}
keyExtractor={(item) => item.id}
/>
</View>
</SafeAreaView>
);
};
export default List;
the error is continuing to be there despite me trying to put navigation container in different positions. I want to send the user to the carProfile page, where the data passed in item is reused. This way user can know about the selection they are looking for
Use this way
onPress={() => this.props.navigation.navigate(
"carProfile",
{item: item}
)}
The navigation is inside the props, not as a second argument. Below is a classic example. And also note, you cannot extract navigation prop for child components, it will only work on Main components(Screens).
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details')}
/>
</View>
);
}
If you want to use navigation inside child components, you can use the navigation hooks.
import { useNavigation } from "#react-navigation/native";
function MyBackButton() {
const navigation = useNavigation();
return (
<Button
title="Back"
onPress={() => {
navigation.goBack();
}}
/>
);
}
When I load my React Native project, I am getting this Error:
A VirtualizedList contains a cell which itself contains more than one VirtualizedList of the same orientation as the parent list. You must pass a unique listKey prop to each sibling list.
How can I fix it?
Error Showing:
Code:
const Home = () => {
function renderRecentEvent() {
return (
<FlatList
data = {recentData}
lisKey = {item => item.id}
showsVerticalScrollIndicator = {false}
renderItem={({item}) => {
return (
<View>
<Text>{ item.event_name }</Text>
</View>
)
}}
/>
)
}
function renderMonthEvent() {
return (
<FlatList
data = {todayData}
lisKey = {item => item.id}
showsVerticalScrollIndicator = {false}
renderItem={({item}) => {
return (
<View>
<Text>{ item.event_info }</Text>
</View>
)
}}
/>
)
}
return (
<View>
<FlatList
data = {eventData}
keyExtractor = {item => item.id}
keyboardDismissMode = 'on-drag'
showsVerticalScrollIndicator = {false}
ListHeaderComponent = {
<View>
{renderRecentEvent()}
{/* --------- Month Event --------- */}
{renderMonthEvent()}
</View>
}
renderItem = { ({item}) => {
return (
<CategoryCard
categoryItem={item}
/>
)
}}
ListEmptyComponent = {
<View
style = {{ marginBottom: 100 }}
/>
}
/>
</View>
)
}
As the error says "You must pass a unique listkey prop to each sibling list".
If there are multiple VirtualizedLists at the same level of nesting within another VirtualizedList, this key is necessary for virtualization to work properly.
For example:
<FlaList>
<FlaList listKey="1.1" />
<FlaList listKey="1.2" />
</FlaList>
<FlaList>
<FlaList listKey="2.1" />
<FlaList listKey="2.2" />
</FlaList>
Update
In your example, you have a typo in the prop name. You have to write listkey not lisKey.
const Home = () => {
function renderRecentEvent() {
return (
<FlatList
data={recentData}
listKey="recent-event"
keyExtractor={item => item.id}
showsVerticalScrollIndicator={false}
renderItem={({ item }) => {
return (
<View>
<Text>{item.event_name}</Text>
</View>
);
}}
/>
);
}
function renderMonthEvent() {
return (
<FlatList
data={todayData}
listKey="month-event"
keyExtractor={item => item.id}
showsVerticalScrollIndicator={false}
renderItem={({ item }) => {
return (
<View>
<Text>{item.event_info}</Text>
</View>
);
}}
/>
);
}
return (
<View>
<FlatList
data={eventData}
keyExtractor={item => item.id}
keyboardDismissMode="on-drag"
showsVerticalScrollIndicator={false}
ListHeaderComponent={
<View>
{renderRecentEvent()}
{renderMonthEvent()}
</View>
}
renderItem={({ item }) => {
return <CategoryCard categoryItem={item} />;
}}
ListEmptyComponent={<View style={{ marginBottom: 100 }} />}
/>
</View>
);
};
I have a FlatList with TouchableOpacity items. When I try to select an item, the list scrolls to top.
This is the Collapsible content;
const setBrandSelected = index => {
var temp = JSON.parse(JSON.stringify(tempFilterOptions));
temp[0].enums[index].selected = !temp[0].enums[index].selected;
setTempFilterOptions(temp);
};
const FilterOptionBrand = () => {
const RenderBrand = ({ item, index }) => {
return (
<TouchableOpacity onPress={setBrandSelected.bind(null, index)}>
{item.selected && (
<View style={[styles.brandImage, styles.selectedBrandImage]}>
<Icon iconName={iconNames.Tick} />
</View>
)}
<FastImage source={{ uri: item.logo }} style={styles.brandImage} resizeMode={FastImage.resizeMode.contain} />
<Text style={styles.brandName}>{item.name}</Text>
</TouchableOpacity>
);
};
const BrandSeparator = () => <View style={styles.brandSeparator} />;
return (
<View style={styles.filterBrandMainContainer}>
<View style={styles.searchBrandContainer}>
<View style={styles.magnifyingGlassWrapper}>
<Icon iconName={iconNames.MagnifyingGlass} />
</View>
<TextInput style={styles.searchBrandInput} placeholder={searchBrandText} />
</View>
<FlatList
horizontal={true}
showsHorizontalScrollIndicator={false}
keyExtractor={(item, index) => String(item.id)}
style={styles.collapsibleContainer}
data={tempFilterOptions[0].enums}
renderItem={RenderBrand}
ItemSeparatorComponent={BrandSeparator}
></FlatList>
</View>
);
};
If you somehow stumble upon this question. The answer is wherever the problem occurs be it Header be it Footer. You should call the component function and not directly just type the function.
const Header = () => <View />
Correct usage;
ListHeaderComponent={Header()}
Incorrect usage;
ListHeaderComponent={Header}