Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript &引用;“意外键”;"富",;在减速器接收到的先前状态下发现”;使用redux工具包配置存储和自定义存储增强器_Javascript_Typescript_Redux_Redux Toolkit - Fatal编程技术网

Javascript &引用;“意外键”;"富",;在减速器接收到的先前状态下发现”;使用redux工具包配置存储和自定义存储增强器

Javascript &引用;“意外键”;"富",;在减速器接收到的先前状态下发现”;使用redux工具包配置存储和自定义存储增强器,javascript,typescript,redux,redux-toolkit,Javascript,Typescript,Redux,Redux Toolkit,我一直在尝试创建一个存储增强器,向我的根状态添加一些新值。然而,在对自定义增强器的所有工作进行仔细研究之后,我发现了一个意想不到的关键错误。我可以在devtools中看到state的新部分,并且应用程序似乎运行良好,但是我得到了 Unexpected key "foo" found in previous state received by the reducer. Expected to find one of the known reducer keys instead

我一直在尝试创建一个存储增强器,向我的根状态添加一些新值。然而,在对自定义增强器的所有工作进行仔细研究之后,我发现了一个意想不到的关键错误。我可以在devtools中看到state的新部分,并且应用程序似乎运行良好,但是我得到了

Unexpected key "foo" found in previous state received by the reducer. 
Expected to find one of the known reducer keys instead: [...]. Unexpected keys will be ignored.
我就是不明白。我试着四处搜索,但没有一篇帖子是关于定制商店增强器的

这是再现上述错误的最小代码。我正在这样创建我的店铺:

export const store=configureStore({
减速器:主减速器,
增强子:[测试员],
});
其中mainReducer只是组合减速机({“我的切片”}),所有切片都是使用RTK中的createSlice创建的

testEnhancer如下所示:

type statext={foo:string}
export const testEnhancer:StoreEnhancer=(下一步:StoreEnhancerStoreCreator):StoreEnhancerStoreCreator=>{
返回(减速器:减速器,预载状态?:预载状态):存储=>{
const wrappedReducer:Reducer=(状态、操作)=>{
返回{…reducer(state,action),foo:'bar'};
};
返回next(wrappedReducer,{…preload状态,foo:'});
};
};

这实际上是特定于组合减速机的行为。它期望在任何顶级状态键和关联的切片缩减器之间有一个永久的1:1映射

您需要以某种方式设置一个自定义减速机来处理此问题,或者根本不使用组合减速机。(有许多类似的版本具有相同的总体行为,但也没有警告。)