reactjs - React 检测到 Hooks 的顺序发生了变化
问题描述
我收到此错误,Warning: React has detected a change in the order of Hooks called by Thread
.
但是我看不到问题,我在函数的顶部调用它,我想我用这段代码有条件地渲染它
if (loading) {
return <Text>{loading ? 'Loading...' : null}</Text>;
}
但是,如果我删除此代码块,我会收到相同的警告。所以我想这不是我的问题。
这是我调用自定义钩子的函数:
import React from 'react';
import {
Box,
Text,
Link,
Container,
useColorModeValue,
} from '@chakra-ui/react';
import { Helmet } from 'react-helmet-async';
import { Link as ReachLink } from 'react-router-dom';
import usePosts from '../hooks/FetchPosts';
function Thread() {
const { thread, loading } = usePosts();
console.log(thread);
if (loading) {
return <Text>{loading ? 'Loading...' : null}</Text>;
}
return (
<Box>
{thread.map((item) => (
<Box key={item._id}>
<Helmet>
<meta charSet='utf-8' />
<title>{`Amicus - ${item.title}`}</title>
</Helmet>
<Container>
<Box>
<Text
as='h1'
fontSize='xx-large'
color={useColorModeValue('brand.100', 'gray.500')}
>
{item.title}
</Text>
<Link
to={`/post/?post=${item._id}`}
as={ReachLink}
style={{ textDecoration: 'none' }}
>
<Box
key={item._id}
border='1px'
borderColor='gray.200'
mt='3'
p='4'
>
<Text
fontWeight='medium'
color={useColorModeValue('brand.100', 'gray.500')}
>
{item.content}
</Text>
<Text
fontWeight='medium'
as='span'
color={useColorModeValue('brand.100', 'gray.500')}
mr='4'
>
Skrivet av: {item.user.username}
</Text>
<Text
fontWeight='medium'
as='span'
color={useColorModeValue('brand.100', 'gray.500')}
mr='4'
>
{item.postdate.slice(0, 10)}
</Text>
<Text
fontWeight='medium'
as='span'
color={useColorModeValue('brand.100', 'gray.500')}
mr='4'
>
{item.posts.length} svar
</Text>
</Box>
</Link>
</Box>
</Container>
</Box>
))}
</Box>
);
}
export default Thread;
这是我的自定义钩子:
import { useState, useEffect } from 'react';
import { useLocation } from 'react-router-dom';
const usePosts = () => {
const [thread, setThread] = useState([]);
const [loading, setLoading] = useState(true);
const { search } = useLocation();
useEffect(() => {
const abortController = new AbortController();
async function getData() {
const URL = `http://localhost:5000/api/v1/categories/${search.slice(9)}`;
try {
const options = {
method: 'GET',
credentials: 'include',
signal: abortController.signal,
};
const fetchData = await fetch(`${URL}`, options);
const res = await fetchData.json();
setThread(res.data.categories.threads);
setLoading(false);
} catch (error) {
if (error.name === 'AbortError') {
console.log('Leaving page before it rendered');
}
console.log(error);
}
}
getData();
}, [URL]);
return { thread, loading };
};
export default usePosts;
解决方案
您需要从条件语句中提取所有挂钩调用
这是你的固定代码
import React from 'react';
import {
Box,
Text,
Link,
Container,
useColorModeValue,
} from '@chakra-ui/react';
import { Helmet } from 'react-helmet-async';
import { Link as ReachLink } from 'react-router-dom';
import usePosts from '../hooks/FetchPosts';
function Thread() {
const { thread, loading } = usePosts();
const color = useColorModeValue('brand.100', 'gray.500');
console.log(thread);
if (loading) {
return <Text>{loading ? 'Loading...' : null}</Text>;
}
return (
<Box>
{thread.map((item) => (
<Box key={item._id}>
<Helmet>
<meta charSet='utf-8' />
<title>{`Amicus - ${item.title}`}</title>
</Helmet>
<Container>
<Box>
<Text
as='h1'
fontSize='xx-large'
color={color}
>
{item.title}
</Text>
<Link
to={`/post/?post=${item._id}`}
as={ReachLink}
style={{ textDecoration: 'none' }}
>
<Box
key={item._id}
border='1px'
borderColor='gray.200'
mt='3'
p='4'
>
<Text
fontWeight='medium'
color={color}
>
{item.content}
</Text>
<Text
fontWeight='medium'
as='span'
color={color}
mr='4'
>
Skrivet av: {item.user.username}
</Text>
<Text
fontWeight='medium'
as='span'
color={color}
mr='4'
>
{item.postdate.slice(0, 10)}
</Text>
<Text
fontWeight='medium'
as='span'
color={color}
mr='4'
>
{item.posts.length} svar
</Text>
</Box>
</Link>
</Box>
</Container>
</Box>
))}
</Box>
);
}
export default Thread;
推荐阅读
- c++ - 可变参数宏中的模板推导失败
- xcode - Catalina - 无法安装 Cocoapods
- c# - 不能隐式转换类型
- c# - 粒子系统,在使用 Play() 和 Stop() 函数时,构建后不起作用
- javascript - 尝试创建一个添加功能,因此当我单击添加按钮时,它会将项目添加到项目符号列表并在其旁边创建一个复选框
- layout - 从 JasperReports Server v 6.3 升级到 v 7.5 后的布局问题
- javascript - Drupal 分类中的国家代码显示为标志
- android - 使用 JNI 从 c++ 回调到 Kotlin:Android
- python - 多个数据帧之间的相关性如何?蟒蛇熊猫
- share - “在 JSON 中,位置 0 处出现意外的令牌 <”,同时实施社交分享短代码