react-native#SectionListRenderItemInfo TypeScript Examples
The following examples show how to use
react-native#SectionListRenderItemInfo.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.tsx From krmanga with MIT License | 4 votes |
function Home({ dispatch, commendList, refreshing, navigation, loading, hasMore }: IProps) {
const headerHeight = useHeaderHeight();
const scrollY: Animated.Value = useRef(new Animated.Value(0)).current;
const [endReached, setEndReached] = useState<boolean>(false);
useEffect(() => {
SplashScreen.hide();//关闭启动屏
dispatch({
type: "home/setState",
payload: {
headerHeight
}
});
syncImmediate();
loadCarouselList();
loadCommendList(true);
}, []);
const syncImmediate = () => {
if (Platform.OS === "android") {
codePush.checkForUpdate().then((update) => {
if (update) {
navigation.navigate("AppUpdate");
}
});
}
};
const loadCarouselList = () => {
dispatch({
type: "home/fetchCarouselList"
});
};
const loadCommendList = (refreshing: boolean, callback?: () => void) => {
dispatch({
type: "home/fetchCommendList",
payload: {
refreshing
},
callback
});
};
const renderSectionHeader = ({ section: { title } }: any) => {
return (
<View style={styles.sectionHeader}>
<View style={styles.cell} />
<Text style={styles.classifyName}>{title}</Text>
</View>
);
};
const onRefresh = () => {
loadCarouselList();
loadCommendList(true);
};
const onEndReached = () => {
if (!hasMore || loading) {
return;
}
setEndReached(true);
loadCommendList(false, () => {
setEndReached(false);
});
};
const renderFooter = () => {
if (endReached) {
return <More />;
}
if (!hasMore) {
return <End />;
}
return null;
};
const goBrief = useCallback((data: IBook) => {
navigation.navigate("Brief", {
id: data.id
});
}, []);
const renderItem = ({ item }: SectionListRenderItemInfo<IBook[]>) => {
return (
<View style={styles.contentContainer}>
{item.map(data => {
return (
<BookCover data={data} goBrief={goBrief} key={data.id} />
);
})}
</View>
);
};
const getTopBarColor = useCallback(() => {
return scrollY.interpolate({
inputRange: [0, maxScroll],
outputRange: [0, 1],
extrapolate: "clamp"
});
}, []);
const TopBarColor = getTopBarColor();
return (
(loading && refreshing) ? <HomePlaceholder /> :
<View style={{ flex: 1 }}>
<CarouselBlurBackground />
<TopBarWrapper navigation={navigation} topBarColor={TopBarColor} />
<SectionList
keyExtractor={(item, index) => `item-${item["id"]}-key-${index}`}
ListHeaderComponent={() => <Carousel />}
renderSectionHeader={renderSectionHeader}
onRefresh={onRefresh}
refreshing={refreshing}
sections={commendList}
stickySectionHeadersEnabled={true}
scrollEventThrottle={1}
onScroll={Animated.event(
[{
nativeEvent: { contentOffset: { y: scrollY } }
}],
{
useNativeDriver: false
}
)}
onEndReached={onEndReached}
onEndReachedThreshold={0.1}
renderItem={renderItem}
extraData={endReached}
ListFooterComponent={renderFooter}
/>
</View>
);
}
Example #2
Source File: index.tsx From krmanga with MIT License | 4 votes |
function History({ dispatch, navigation, isLogin, isEdit, historyList, ids, loading, refreshing, hasMore, pages }: IProps) {
const translateX: Animated.Value = useRef(new Animated.Value(0)).current;
const [endReached, setEndReached] = useState<boolean>(false);
useFocusEffect(
React.useCallback(() => {
loadData(true);
return () => {
dispatch({
type: "history/setState",
payload: {
isEdit: false,
ids: []
}
});
};
}, [isLogin])
);
const loadData = (refreshing: boolean, callback?: () => void) => {
if (isLogin) {
dispatch({
type: "history/fetchHistoryList",
payload: {
refreshing: refreshing
},
callback
});
}
};
const renderSectionHeader = ({ section: { title } }: any) => {
return (
<View style={styles.headerView}>
<Text style={styles.headerTitle}>{title}</Text>
</View>
);
};
const getBeforeX = () => {
Animated.timing(translateX,
{
useNativeDriver: false,
toValue: wp(5),
duration: 150
}
).start();
};
const getAfterX = () => {
Animated.timing(translateX,
{
useNativeDriver: false,
toValue: 0,
duration: 150
}
).start();
};
const onClickItem = useCallback((item: IHistory[]) => {
if (isEdit) {
let i = ids.indexOf(item["book_id"]);
if (i > -1) {
ids.splice(i, 1);
dispatch({
type: "history/setState",
payload: {
ids: [...ids]
}
});
} else {
dispatch({
type: "history/setState",
payload: {
ids: [...ids, item["book_id"]]
}
});
}
} else {
navigation.navigate("Brief", {
id: item["book_id"]
});
}
}, [isEdit, ids]);
const goMangaView = useCallback((item: IHistory[]) => {
navigation.navigate("Brief", {
id: item["book_id"]
});
navigation.navigate("MangaView", {
chapter_num: item["chapter_num"],
markRoast: item["roast"],
book_id: item["book_id"]
});
}, []);
const renderItem = ({ item }: SectionListRenderItemInfo<IHistory[]>) => {
const selected = ids.indexOf(item["book_id"]) > -1;
return (
<Touchable
key={item["id"]}
onPress={() => onClickItem(item)}
>
<Animated.View
style={{ transform: [{ translateX: translateX }] }}
>
<Item
data={item}
isEdit={isEdit}
selected={selected}
goMangaView={goMangaView}
/>
</Animated.View>
</Touchable>
);
};
const onRefresh = () => {
dispatch({
type: "history/setScreenReload"
});
loadData(true);
};
const onEndReached = () => {
if (!hasMore || loading) {
return;
}
setEndReached(true);
loadData(false, () => {
setEndReached(false);
});
};
const renderFooter = () => {
if (endReached) {
return <More />;
}
if (!hasMore) {
return <End />;
}
return null;
};
const cancel = () => {
let newData: string[] = [];
historyList.forEach(items => {
items.data.forEach(item => {
newData = newData.concat(item["book_id"]);
});
}
);
if (newData.length === ids.length) {
dispatch({
type: "history/setState",
payload: {
ids: []
}
});
} else {
dispatch({
type: "history/setState",
payload: {
ids: newData
}
});
}
};
const destroy = () => {
dispatch({
type: "history/delUserHistory",
payload: {
ids
}
});
};
if (isEdit) {
getBeforeX();
} else {
getAfterX();
}
return (
!isLogin ? null :
(loading && refreshing) ? <ListPlaceholder /> :
<View style={styles.container}>
<SectionList
keyExtractor={(item, index) => `section-item-${index}`}
renderSectionHeader={renderSectionHeader}
onRefresh={onRefresh}
refreshing={refreshing}
sections={historyList}
style={styles.container}
stickySectionHeadersEnabled={true}
onEndReached={onEndReached}
onEndReachedThreshold={0.1}
renderItem={renderItem}
extraData={endReached}
ListFooterComponent={renderFooter}
/>
<EditView
data_length={pages.current_page * pages.page_size < pages.total ?
pages.current_page * pages.page_size : pages.total}
isEdit={isEdit}
ids={ids}
cancel={cancel}
destroy={destroy}
/>
</View>
);
}