“useContext“ is called in function “index“

React Hook “useContext” is called in function “index” that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word “use”

报错

Line 12:23: React Hook “useContext” is called in function “index” that is neither a React function component nor a custom React Hook function. React component names
must start with an uppercase letter. React Hook names must start with the word “use” react-hooks/rules-of-hooks
Line 13:43: React Hook “useState” is called in function “index” that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word “use” react-hooks/rules-of-hooks
Line 15:5: React Hook “useEffect” is called in function “index” that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word “use” react-hooks/rules-of-hooks

环境

  • react版本:18.2.0
  • 打包工具:webpack

事故现场

import React, { useContext, useEffect, useState } from 'react';
import { globalContext } from '../../App';

export default function index(props: props) {

    const { theme } = useContext(globalContext)!
    const [uploadState, setUploadState] = useState(false);

    useEffect(()=>{
      setUploadState(upload);
    },[upload]);

  return (
    <div><div/>
  )
}

事故分析

报错的意思是你的useState、useEffect、useEffect在这里的用法都是错误的,既不是react定义的hook,也不是你自定义的hook。
但是我react的版本是18.2.0,怎么也不该不能用useState这些的。
后来我琢磨了一下,更准确的原因是你这个index函数不是一个react的函数组件也不是一个react的钩子函数,所以你在这里的useState的用法是错误的。

事故原因

export default function index()

解决方法

export default function EPie()

解决方法分析

不能直接用index给你的函数组件命名,即使直接生成的函数组件的名字是index,你也得改成其他的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值