【发布时间】:2020-01-14 21:52:58
【问题描述】:
首先,我真的很陌生;所以,对于初学者的问题,我们深表歉意。
我有一个带有 Redux 和 Redux Saga 的 React 应用程序。
其中一个组件如下所示:
import { TableContainer, TableHead, TableRow } from '@material-ui/core';
import Paper from '@material-ui/core/Paper';
import makeStyles from '@material-ui/core/styles/makeStyles';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import { ProgramCategory } from '../model/program-category';
import { ProgramCategoryItemRow } from '../ProgramGategoryItemRow/ProgramCategoryItemRow';
import { ProgramCategoryActions } from '../store/program-category.actions';
import { ProgramCategorySelectors } from '../store/program-category.selectors';
const useStyles = makeStyles({
table: {
width: '100%',
},
tableHeadCell: {
fontWeight: 'bold',
},
});
export interface ProgramCategoriesTableProps {
isLoaded: boolean;
categories: ProgramCategory[];
fetchAllCategories: () => void;
}
export const PureProgramCategoriesTable: React.FC<ProgramCategoriesTableProps> = ({
isLoaded,
categories,
fetchAllCategories,
}) => {
useEffect(() => {
console.error('in useEffect');
fetchAllCategories();
});
const styles = useStyles();
return (
<TableContainer component={Paper}>
// the rest
<TableBody>
{categories.map(c => (
<ProgramCategoryItemRow category={c} />
))}
</TableBody>
</TableContainer>
);
};
const mapStateToProps = createSelector(
[ProgramCategorySelectors.isLoaded, ProgramCategorySelectors.getAll],
(isLoaded, categories) => ({ isLoaded, categories }),
);
const mapDispatchToProps = {
fetchAllCategories: ProgramCategoryActions.fetchAll.start,
};
export const ProgramCategoriesTable = connect(mapStateToProps, mapDispatchToProps)(PureProgramCategoriesTable);
处理ProgramCategoryActions.fetchAll.start的sagas如下:
import { call, put, takeLatest } from 'redux-saga/effects';
import { ProgramCategoryApi } from '../services/program-category.api';
import { ProgramCategoryActions } from './program-category.actions';
function* handleFetchAll() {
try {
const categories = yield call(ProgramCategoryApi.fetchAll);
yield put(ProgramCategoryActions.fetchAll.success(categories));
} catch (e) {
yield put(ProgramCategoryActions.fetchAll.failure(e));
}
}
export function* programCategorySagas() {
yield takeLatest(ProgramCategoryActions.fetchAll.start.type, handleFetchAll);
}
一切都有意义,但是我的操作代码会一遍又一遍地执行。再深入研究一下,效果挂钩似乎也被一遍又一遍地执行。 如果我理解正确,这是因为状态中的数据正在发生变化,组件再次重新渲染。但是,它会导致无限循环。 我究竟做错了什么?设置这种组件的正确方法是什么?
我发现的一个选项是将传奇更改为:
function* handleFetchAll() {
try {
const alreadyLoaded = select(ProgramCategorySelectors.isLoaded);
if (!alreadyLoaded) {
const categories = yield call(ProgramCategoryApi.fetchAll);
yield put(ProgramCategoryActions.fetchAll.success(categories));
}
} catch (e) {
yield put(ProgramCategoryActions.fetchAll.failure(e));
}
}
所以,它只调用一次 api;它似乎以这种方式工作正常。但是,这是正确的解决方案吗?
按照 cmets 中的建议,我尝试在效果中添加依赖项:
useEffect(() => {
fetchAllCategories();
}, []);
现在,我收到一个错误:
./src/program/ProgramCategoriesTable/ProgramCategoriesTable.tsx 行 37:6:React Hook useEffect 缺少依赖项: 'fetchAllCategories'。包含它或删除依赖项 大批。如果 'fetchAllCategories' 更改过于频繁,请查找父级 定义它并将该定义包装在 useCallback 中的组件 react-hooks/exhaustive-deps
【问题讨论】:
-
useEffect在每次渲染后被调用。如果您的代码内部导致重新渲染,这将导致无限循环。考虑为您的效果添加依赖项或条件 -
例如。如果您只想获取一次。提供一个空的依赖数组:
useEffect(() => {fetchStuff()}, [])。或者仅当 myVar 发生变化时:useEffect(() => {fetchStuff()}, [myVar]) -
我猜测触发重新渲染的位置是您的
mapStateToProps。您在这里正确使用createSelector()吗?我个人对reselect不熟悉,但从their README 我看不到与您类似的示例。如果你还没有,也许从那里开始。 -
@BrianThompson 添加了更多信息
-
它希望您将函数作为依赖项提供,这是不必要的,因为它不会改变。但为了让它开心,你可以做
useEffect(() => {fetchAllCategories()}, [fetchAllCategories])
标签: javascript reactjs redux redux-saga