avatar

sunday

Sunday's Blog

  • 首页
主页 next.js中使用zustand引入第三方库报错self is not defined
文章

next.js中使用zustand引入第三方库报错self is not defined

发表于 2024-01-21 更新于 2024-01- 21
作者 sunday 已删除用户
3~3 分钟 阅读

nextjs 14.0.4使用zustand引用一些第三方的库时出现的self is not defined 问题

比如useExamplePKGStore

import examplePKG from "example";
const useExamplePKGStore = create((set) => ({
	data: null;
	init: ()=> {
		set({ data: new examplePKG() })
	}
}))

这时候在next的页面中引用

任意的page.js

import useExamplePKGStore from '@/app/store/useExamplePKGStore';
import { useEffect } from 'react';
const Page = () => {
	const { init } = useExamplePKGStore()
	useEffect(()=>{
		init()
    }, [])
   return <div>test</div>	
}

这时候会报self is not defined

在useExamplePKGStore.js 中改为

import examplePKG from "example";
const useExamplePKGStore = create((set) => ({
	data: null;
	init: ()=> {
       import("example").then((examplePKG) => {
		   set({ data: new examplePKG() })
       });
	}
}))

即可解决组件中报self is not defined 的问题

疑难杂症, nextjs
nextjs
许可协议:  CC BY 4.0
分享

相关文章

8月 11, 2025

导致DNS泄露的一些问题

1.首先检测你的设备是否有DNS泄露问题 开启代理的情况下,去这个网站https://ipleak.net/ 看一下是否有显示国内的IP,有的话就是DNS泄露了

7月 25, 2025

解决vscode上python代码无法解析导入

1.已经生成了虚拟环境了,如以下结构 ⏺ your-project/ ├── .gitignore ├── api/ │ └── index.py ├── requirements.txt └── .venv/ └── (虚拟环境文件) 2.vscode上显示如下

3月 27, 2025

解决nextjs15使用useLocalStorage报错的问题

已经在组件中使用"use client"声明了,还是报错,错误如下: Unhandled Runtime Error Error: useLocalStorage is a client-only hook 1.解决方案1-使用 dynamic 导入 import dynamic from 'nex

下一篇

解决国内安装docker-compose很慢很卡安装不了的问题

上一篇

浏览器出现的一些跨域的问题,如何正确设置cors

最近更新

  • ios18 swiftUI 开发的一些问题
  • Cursor IDE中开发IOS应用——支持热更新
  • nginx + acme 不占用80端口申请证书
  • 免费CDN 阿里云ESA 加速国内网站
  • nextjs15使用ai sdk的一些问题

热门标签

nginx acme 强制跳转HTTPS nodejs 代理 mac 神器 vue3 工具 docker

目录

©2025 sunday. 保留部分权利。

使用 Halo 主题 Chirpy