【发布时间】:2021-11-02 08:55:22
【问题描述】:
在我的组件中使用自定义钩子返回值时,我不断收到“渲染的钩子比上一次渲染时更多”错误
钩子是这样的
import { useState, useEffect } from 'react';
import axios from 'axios';
const { REACT_APP_BASE_PATH } = process.env;
axios.defaults.baseURL = REACT_APP_BASE_PATH;
export const useAxios = (axiosParams, deps = []) => {
const [response, setResponse] = useState({});
const [error, setError] = useState('');
const [loading, setLoading] = useState(true);
const fetchData = async (params) => {
try {
const result = await axios.request(params);
setResponse(result.data);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchData(axiosParams);
}, [...deps]);
return { response, error, loading };
};
export default { useAxios };
组件如下所示:
import React from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import { Box, Button, Typography } from '@material-ui/core';
import CircularProgress from '@material-ui/core/CircularProgress';
import { useAxios } from '../../helpers/hooks';
import { accountsListColumns, renderItems } from '../../helpers/constants';
import useStyles from '../styles';
export const SingleAccountPage = () => {
const { search } = useLocation();
const history = useHistory();
const classes = useStyles();
const {
response = [],
error,
loading,
} = useAxios({
method: 'GET',
url: `/account/findOne${search}`,
});
if (loading) return <CircularProgress />;
if (error) return <h1>{error}</h1>;
return (
<Box className={classes.singlePageWrapper}>
<Box className={classes.singlePageHeader}>
<Typography variant={'h4'}>Account: {response.id}</Typography>
<Button
variant={'contained'}
onClick={() => history.goBack()}
className={classes.singlePageBackButton}
>
Go back
</Button>
</Box>
{renderItems(accountsListColumns, response)}
</Box>
);
};
所以基本上这两行由于某种原因导致了问题
if (loading) return <CircularProgress />;
if (error) return <h1>{error}</h1>;
将来,我想有条件地渲染错误并加载组件,但现在由于某种原因,即使这样也行不通。
在错误消息中,它说
index.js:1 Warning: React has detected a change in the order of Hooks called by SingleAccountPage. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks: https://reactjs.org/link/rules-of-hooks
Previous render Next render
------------------------------------------------------
1. useContext useContext
2. useContext useContext
3. useContext useContext
4. useDebugValue useDebugValue
5. useContext useContext
6. useRef useRef
7. useRef useRef
8. useRef useRef
9. useMemo useMemo
10. useEffect useEffect
11. useEffect useEffect
12. useDebugValue useDebugValue
13. useState useState
14. useState useState
15. useState useState
16. useEffect useEffect
17. undefined useContext
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
和
Uncaught Error: Rendered more hooks than during the previous render.
你能帮我解决这个问题吗?
【问题讨论】:
-
这真的很奇怪。我建议您尝试在代码和框中创建一个最小的可重现示例并在那里产生相同的错误(这将帮助您和我们缩小问题的范围)。你有点让我有兴趣找出问题所在。目前,在您提供的代码中,我没有看到任何内容。 (您的早期退货声明没有任何问题,因此尽管删除它可以解决问题,但这可能不是这里的核心问题)。目前,我唯一的怀疑是你正在导入的 useStyles 钩子。你能分享一下代码吗?或者评论一下看看会发生什么?
-
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。
标签: reactjs react-hooks material-ui