fork Notistack二次开发无法导出Redux store报错求解决方案
核心错误修复
1. 导出不匹配导致的编译报错
你当前应用用的是默认导入语法import NotisReducer from 'notistack',需要在fork的Notistack库的index.js中添加对应的默认导出配置:
// Notistack库的index.js // 注意此处导入的是你写的reducer纯函数,不是createStore生成的store实例 import notistackReducer from './Redux/NotisReducer' // 原有其他导出逻辑保留 // 新增默认导出 export default notistackReducer
如果你不想用默认导出,把应用里的导入改成命名导入即可:import { NotisReducer } from 'notistack',同时在Notistack的index.js里写export const NotisReducer = notistackReducer。
2. Redux概念混淆逻辑错误
combineReducers接收的是纯函数类型的reducer,不是你通过createStore/configureStore生成的store实例,你现在把store实例放到合并逻辑里属于用法错误,两种修正方案可选:
- 方案一(推荐):上层应用维护全局store,只把Notistack的reducer函数导出给上层合并到根reducer,Notistack的组件直接从全局store读取状态、响应dispatch
- 方案二:Notistack内部独立维护store,直接导出store实例给上层,上层直接调用
import { notistackStore } from 'notistack',用notistackStore.dispatch提交更新,不需要把store放到根reducer里
3. Typescript类型定义错误
你写的export type store = store属于循环非法定义,且混淆了值和类型的声明,正确写法参考:
// Notistack的index.d.ts import notistackReducer from './Redux/NotisReducer' // 导出reducer对应状态的类型 export type NotistackState = ReturnType<typeof notistackReducer> // 导出store实例的类型(如果用方案二) import store from './Redux/store' export type NotistackStore = typeof store // 声明导出的值的类型 export declare const NotisReducer: typeof notistackReducer export declare const notistackStore: typeof store
不需要在SnackbarProvider.tsx重复导出,所有公共导出统一在index.js和index.d.ts中声明即可。
4. npm link调试配置
修改Notistack源码后需要先执行打包命令(通常是npm run build)才会同步到link的测试应用,要热更可以开启Notistack的watch打包模式;调试可以直接在Chrome devtools的source面板找到webpack://下的Notistack源码打断点,不需要依赖VSCode断点。
内容的提问来源于stack exchange,提问作者Kid
相关产品推荐
相关产品推荐

