reactjs - 有没有办法在不调用 useEffect 中的返回函数的情况下重新渲染组件?
问题描述
我是 React Hooks 的新手;我正在使用带有 react-navigation 的 React native,我正在使用 Redux 来管理状态,一切都很顺利,但是当我尝试使用 onEndReached FlatList 在内部加载更多数据时,如果我尝试清除状态,我正在更改状态使用 useEffect 中的返回函数,组件重新渲染并给我新数据,但是如果我删除 useEffect 中的返回函数,则按预期工作;请我乞求你的帮助。
这是我的代码:
动作.js:
// Clear Category
export const clearGetCategoryScreen = () => {
return { type: CLEAR_CATEGORIES_SCREEN, payload: [] };
};
减速器:
const initialState = {};
export default (state = initialState, action) => {
switch (action.type) {
case GET_CATEGORY:
return { ...state, category: action.payload };
case CLEAR_CATEGORIES_SCREEN:
return { ...state, category: action.payload };
default:
return state;
}
};
这是我的组件:
import React, { useEffect, useState, useContext } from "react";
import { FlatList, View, StyleSheet, ActivityIndicator } from "react-native";
import { StackNavigationProp } from "@react-navigation/stack";
import { useTheme } from "react-native-paper";
import { Twitt } from "../../../components/twitt";
import { CategoryWrapper } from "./CategoryWrapper";
import { StackNavigatorParamlist } from "../../../types";
import { NavigationContext, useFocusEffect } from "@react-navigation/native";
import { useSelector, useDispatch } from "react-redux";
import { getCategoryByID } from "../../../actions/posts_action.js";
import { CLEAR_CATEGORIES_SCREEN } from "../../../actions/types";
type TwittProps = React.ComponentProps<typeof Twitt>;
function renderItem({ item }: { item: TwittProps }) {
return <CategoryWrapper {...item} />;
}
function keyExtractor(item: TwittProps) {
return item.id.toString();
}
type Props = {
navigation?: StackNavigationProp<StackNavigatorParamlist>;
};
export const CategoryList = (props: Props) => {
const theme = useTheme();
const [perPage, setPerPage] = useState(5);
const [isLoading, setIsLoading] = useState(false);
const posts = useSelector((state) => state.category.category);
const dispatch = useDispatch();
const navigation = useContext(NavigationContext);
const id = props.id;
useEffect(() => {
setIsLoading(false);
dispatch(getCategoryByID(id, perPage));
setIsLoading(true);
return () => {
dispatch({ type: CLEAR_CATEGORIES_SCREEN });
};
})
const dataToRender = posts
? posts.map((postProps) => ({
...postProps,
onPress: () =>
navigation &&
navigation.push("Details", {
...postProps,
}),
}))
: null;
const renderFooter = () => {
return isLoading ? (
<View style={styles.loader}>
<ActivityIndicator size="large" color={theme.colors.primary} />
</View>
) : null;
};
return (
<FlatList
contentContainerStyle={{ backgroundColor: theme.colors.background }}
style={{ backgroundColor: theme.colors.background }}
data={dataToRender}
renderItem={renderItem}
keyExtractor={keyExtractor}
ItemSeparatorComponent={() => (
<View style={{ height: StyleSheet.hairlineWidth }} />
)}
onEndReached={() => {
setPerPage(perPage + 3);
}}
showsVerticalScrollIndicator={false}
onEndReachedThreshold={0.1}
ListFooterComponent={renderFooter()}
/>
);
};
const styles = StyleSheet.create({
loader: {
margin: 10,
alignItems: "center",
},
});
解决方案
推荐阅读
- ffmpeg - 使用 ffmpeg 下载 Internet 站点上发布的 HLS 格式视频时的间隔选项
- javascript - 如何在 VUE 3 上使用 VivaGraphJS 在浏览器上显示图表?
- javascript - 在网站上存储特定字符串的最佳方式是什么?
- frama-c - “默认行为:尝试使用 Frama-C 内核”是什么意思?意思是?
- c# - 使用我的 C# 项目获取值不能为空错误
- rust - 如何在构造函数中调用结构方法?
- android - 当 Recycler 在 Fragment 中滚动时隐藏和显示 Fab
- linux - 如何在linux中安装TSC TA210条码打印机驱动
- bash - 打印偶数和奇数
- android - 将应用从个人资料安装到工作资料