因为疫情, 最近接手一个新项目。是React的。上次写React已经过去1年多了。
虽然捡起来也不是什么难事,不过技术这个东西,长时间不用就容易忘记。
为了保证这个项目根其他平行项目的技术栈统一。
采用的是 Nextjs 、styled-components、useContext 、react-query、ts
今天不做重点介绍项目,还是说说在项目中碰到的问题。
这个足足折腾了差不多2个小时,我一直在排查其问题。
最后这个问题也是比较的诡异。
ReferenceError: Cannot access 'Context' before initialization This error happened while generating the page. Any console logs will be displayed in the terminal window.
引用错误 , 不能在初始化之前访问Context
, 在生成页面的时候就已经发生。在Shell控制台也有显示输出。
我尝试过很多的办法, 例如:换用引用的方式、多个Provider
的位置调整,甚至只保留一个 , 依然无法解决。
后来我试试可能组建声明的类型问题。
我平时对于写组建的方式比较随意。
最喜欢的一种方式就是
import { useState , createContext} from 'react'
import Me from './me'
export const MyContext = createContext({});
export default function Demo(){
const [say , setSay] = useSta