自学内容网 自学内容网

React 常见的报错及解决方法

1、Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons(无效的钩子调用。钩子只能在函数组件的内部调用。这可能是由于以下原因之一)
原因:Hook Api只能在写成组件的主体里面。
解决方法:

import { useNavigate } from "react-router-dom";
const Navigate1 = useNavigate(); // 写在这里就是错的
export default Home () {
const Navigate2 = useNavigate(); // 写在这里就是正确的
return (
<div>home</div>
);
}

2、Unknown property ‘class’ found, use ‘className’ instead(class属性是未知的,用className代替)
原因:react中要使用className属性来表示类名。

<div className="header active">hello world</div>

3、Warning: Functions are not valid as a React child. This may happen if you return a Component(函数作为React的子组件是无效的)
原因:调用某个方法时忘记加括号
解决办法:就是在方法名后面加上括号()


原文地址:https://blog.csdn.net/qq_39460057/article/details/140669625

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!