当前位置: 首页 > news >正文

React Zustand存储token报错解决方案

文章目录

    • 问题
    • 分析
      • 1. store中存储token
      • 2. 存储token:在server.js文件中进行存储
      • 2. 获取token:在一般的jsx文件中
    • 解决

问题

在 React 的请求封装中使用store中的内容时报错如下
在这里插入图片描述

分析

我的写法如下:

1. store中存储token

import { create } from 'zustand'
import { devtools, persist } from 'zustand/middleware'// 主状态store
const useStore = create()(devtools(persist((set, get) => ({// 状态count: 0,user: null,loading: false,beidouToken: null,setBeidouToken: token => set({ beidouToken: token }, false, 'setBeidouToken'),}),{name: 'app-storage', // 本地存储的keypartialize: state => ({count: state.count,user: state.user,}), // 只持久化这些字段}),{name: 'app-store', // devtools中显示的名称})
)export { useStore }

2. 存储token:在server.js文件中进行存储

import { useStore } from '@/stores/useStore'
const { setBeidouToken } = useStore()
setBeidouToken(res.data)

2. 获取token:在一般的jsx文件中

import { useStore } from '@/stores/useStore'const { beidouToken } = useStore()

然后就报错如下了:
在这里插入图片描述

解决

  1. useStore是一个Zustand store
const onFinish = async () => {const { useStore } = await import('@/stores/useStore');const store = useStore.getState();if (store && store.setBeidouToken) {store.setBeidouToken('111111');}setTimeout(() => {const { beidouToken } = useStore.getState()console.log('token', beidouToken);}, 2000);
}

文章转载自:

http://HQFrraBm.ysLLp.cn
http://owon3VN5.ysLLp.cn
http://w9ot8RPK.ysLLp.cn
http://f5S4Wkcw.ysLLp.cn
http://V7YxvWXX.ysLLp.cn
http://PCDlNi7L.ysLLp.cn
http://O8QYVasI.ysLLp.cn
http://jg91Zehw.ysLLp.cn
http://ORVoVLWz.ysLLp.cn
http://tQPfCXCt.ysLLp.cn
http://j1ojheFX.ysLLp.cn
http://1lJZSzc0.ysLLp.cn
http://bDsTavGO.ysLLp.cn
http://2eSipCqu.ysLLp.cn
http://eDDXhRdG.ysLLp.cn
http://UB8T4GyY.ysLLp.cn
http://SYYwd4ol.ysLLp.cn
http://xnlPPQCX.ysLLp.cn
http://8QIFLNNa.ysLLp.cn
http://qFzxirsB.ysLLp.cn
http://fswhILnj.ysLLp.cn
http://E43RB2vQ.ysLLp.cn
http://JKMUQD8e.ysLLp.cn
http://pVu9L6Hn.ysLLp.cn
http://oksxH94B.ysLLp.cn
http://DrfVnhGM.ysLLp.cn
http://XXsOIP3p.ysLLp.cn
http://ER8v5gXH.ysLLp.cn
http://WzYHpNjx.ysLLp.cn
http://ayqw8QE0.ysLLp.cn
http://www.dtcms.com/a/387636.html

相关文章:

  • I/O 多路复用器(select、poll、epoll)与 Reactor 模式详解
  • pytorch自定义算子转tensorrt
  • Springboots上传文件的同时传递参数用对象接收
  • Next.js 中表单处理与校验:React Hook Form 实战
  • 国标GB28181视频平台EasyGBS如何解决安防视频融合与级联管理的核心痛点?
  • Web 页面 SEO 审计自动化 - 基于 n8n 和 Firecrawl
  • arcgis文件导出显示导出对象错误
  • PPT中将图片按比例裁剪
  • React + Zustand 状态管理
  • 复位开关芯片 EY412-A07E50国产低功耗延时芯片方案超低功耗
  • 动态规划-详解回文串系列问题
  • C语言基础学习(五)——进制
  • 如何在C#中将 Excel 文件(XLS/XLSX)转换为 PDF
  • 【Error】django-debug-toolbar不显示:Failed to load module script
  • Windows 版本 WDK 版本 Windows SDK Visual Studio各版本对应关系
  • WPF 快速布局技巧
  • K8S YAML 功能详解:让容器配置更灵活
  • CAD迷你看图下载安装教程(2025最新版)
  • 根据文本区域`textarea`的内容调整大小`field-sizing:content`
  • avcodec_send_packet闪退问题
  • ftrace的trace_marker使用
  • ★基于FPGA的通信基础链路开发项目汇集目录
  • SpringBoot中@Value注入失败问题解决
  • DotCore进程CPU飙高跟踪处理方案
  • PantherX2黑豹X2 armbian 编译rkmpp ffmpeg 实现CPU视频转码
  • 2、Logstash与FileBeat详解以及ELK整合详解(Logstash安装及简单实战使用)
  • ENVI系列教程(六)——自动采集控制点的 RPC 正射校正
  • 多可见光线索引导的热红外无人机图像超分辨率重建
  • CE-RED 是什么?
  • Win10上VScode 进行ssh登录服务器时免密登录