Javascript &引用;错误:在执行减速机时,不能调用store.getState();
我刚刚将功能齐全的react本机应用程序升级到Redux v4,但现在出现以下错误: Error:Error:Error:Error:reducer正在执行时,您不能调用store.getState()。减速器已将状态作为参数接收。从顶部减速器向下传递,而不是从存储中读取 我怀疑问题在于我在其他组件中有许多组件,每个组件都有自己的Javascript &引用;错误:在执行减速机时,不能调用store.getState();,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我刚刚将功能齐全的react本机应用程序升级到Redux v4,但现在出现以下错误: Error:Error:Error:Error:reducer正在执行时,您不能调用store.getState()。减速器已将状态作为参数接收。从顶部减速器向下传递,而不是从存储中读取 我怀疑问题在于我在其他组件中有许多组件,每个组件都有自己的连接(MapStateTops,mapDispatchToProps)(组件),我认为这不是实现它的正确方法,尽管我不确定正确的方法 任何方向都非常感谢 堆栈跟踪:
连接(MapStateTops,mapDispatchToProps)(组件)
,我认为这不是实现它的正确方法,尽管我不确定正确的方法
任何方向都非常感谢
堆栈跟踪:
This error is located at:
in Connect(SideBarApp) (at SceneView.js:9)
in SceneView (at createTabNavigator.js:10)
in RCTView (at View.js:43)
in RCTView (at View.js:43)
in ResourceSavingScene (at createBottomTabNavigator.js:86)
in RCTView (at View.js:43)
in RCTView (at View.js:43)
in TabNavigationView (at createTabNavigator.js:127)
in NavigationView (at createNavigator.js:59)
in Navigator (at createNavigationContainer.js:376)
in NavigationContainer (at SceneView.js:9)
in SceneView (at SwitchView.js:12)
in SwitchView (at createNavigator.js:59)
in Navigator (at createNavigationContainer.js:376)
in NavigationContainer (at AppNavigator.js:36)
in App (created by Connect(App))
in Connect(App) (at index.ios.js:23)
in Provider (at index.ios.js:22)
in TheNewsApp (at renderApplication.js:32)
in RCTView (at View.js:43)
in RCTView (at View.js:43)
in AppContainer (at renderApplication.js:31)
This error is located at:
in NavigationContainer (at SceneView.js:9)
in SceneView (at SwitchView.js:12)
in SwitchView (at createNavigator.js:59)
in Navigator (at createNavigationContainer.js:376)
in NavigationContainer (at AppNavigator.js:36)
in App (created by Connect(App))
in Connect(App) (at index.ios.js:23)
in Provider (at index.ios.js:22)
in TheNewsApp (at renderApplication.js:32)
in RCTView (at View.js:43)
in RCTView (at View.js:43)
in AppContainer (at renderApplication.js:31)
This error is located at:
in NavigationContainer (at AppNavigator.js:36)
in App (created by Connect(App))
in Connect(App) (at index.ios.js:23)
in Provider (at index.ios.js:22)
in TheNewsApp (at renderApplication.js:32)
in RCTView (at View.js:43)
in RCTView (at View.js:43)
in AppContainer (at renderApplication.js:31)
getState@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:79579:24
runComponentSelector@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:78896:56
initSelector@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:79019:28
Connect(SideBarApp)@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:78969:29
constructClassInstance@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:20826:32
updateClassComponent@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:22393:35
performUnitOfWork@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:24922:27
workLoop@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:24955:47
renderRoot@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:24988:21
performWorkOnRoot@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25549:23
performWork@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25481:30
performSyncWork@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25456:20
requestWork@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25362:26
scheduleWork@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25224:28
enqueueSetState@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:20681:23
setState@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:2380:37
dispatch@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:80901:27
navigate@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:80517:24
nav@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:80470:44
combination@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:79810:38
dispatch@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:79628:38
setLoginStatus@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:112247:19
http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:112199:44
tryCallOne@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8818:16
http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8919:27
_callTimer@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8162:17
_callImmediatesPass@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8198:19
callImmediates@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:8417:33
__callImmediates@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7741:32
http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7580:34
__guard@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7721:15
flushedQueue@http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7579:21
flushedQueue@[native code]
invokeCallbackAndReturnFlushedQueue@[native code]
在我的情况下,我必须删除一个chrome插件
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
// const enhancer = composeWithDevTools(applyMiddleware(thunk))
const enhancer = applyMiddleware(thunk)
const store = createStore(reducers, enhancer);
我在使用polymer 3的PWA构建时也犯了同样的错误。 store.js还尝试使用我不得不停用的redux开发工具:
。。。
//为时间旅行调试设置Chrome扩展。
//看https://github.com/zalmoxisus/redux-devtools-extension 了解更多信息。
//const devCompose=window.uu REDUX_DEVTOOLS_EXTENSION_ucompose_u124;COMPOSE;
const devCompose=compose;
//使用LazyReducerHancer初始化Redux存储(以便
//在创建存储后延迟添加reducer)和redux thunk(因此
//您可以调度异步操作)。请参阅“Redux和状态管理”
//有关更多详细信息,请参阅wiki部分:
// https://github.com/Polymer/pwa-starter-kit/wiki/4.-Redux-and-state-management
export const store=createStore(
state=>state,
开发公司(
懒散减速机(组合减速机),
applyMiddleware(thunk))
);
...
这是redux devtools扩展的新的v2.16.0版本的一个问题。
在我们等待修复时,这里有两个解决方法
- 下载
- 拉开拉链
- 类型<代码>chrome://extensions进入url,并在页面的
上切换右上方
开发者模式
- 此时会出现按钮
。单击按钮后,选择提取的文件夹Load unpacketed
- 在浏览器级别或通过代码(在其中创建redux存储)
供参考:这并不能解决OP的问题,但可以解决开发者从2018年11月27日开始收到以下错误消息的问题
错误:在执行减速机时,不能调用store.getState()。减速器已将状态作为参数接收。从顶部减速器向下传递,而不是从存储中读取。
更新 v2.16.2已经发布
对于那些以前禁用过扩展的人,只需重新启用它,并在我的项目中将redux开发工具从2.16.0更新到2.16.2即可。有一天,这个问题不知从哪里冒出来 我的解决方案:禁用Chrome扩展-Redux开发工具。 然后一切都恢复正常
因此,对于这种错误,您应该在多个浏览器中进行测试以找到问题。我就是这么做的:刚刚在我的store.js文件中注释了Chrome Redux Devtools扩展名的行
....
const store = createStore(
rootReducer,
initialState,
compose(
applyMiddleware(...middleware)
///This line--> window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
);
....
这个问题一小时前才开始。我们都知道扩展在开发过程中非常方便,所以让我们等待权威机构的真正修复。禁用chrome扩展或从代码中删除
composeWithDevTool
将是一个快速修复。但我们都知道,为了跟踪应用程序状态并正确管理它,我们需要扩展。所以我今天创造了一个问题,请支持,希望redux团队的人会回来找我们
或者,如果您正在寻找一个临时解决方案(针对chrome),您可以下载它,然后将其解压缩到某个文件夹中
类型chrome://extensions 从左上角打开开发者模式,然后单击LoadUnpacket并选择要使用的解压缩文件夹
问题:适合我的解决方案 禁用Redux开发工具的Chrome扩展。或者从代码中删除记录器 更新:将redux开发工具从2.16.0更新到2.16.1
.现在应该修复。将redux开发工具从2.16.0更新到2.16.1
在我的例子中,问题与开发扩展无关。我在使用深度链接时遇到了这个错误——在reducer中更新路由查询。与在settimeout中操作url相关的包装逻辑修复了错误。我在Chrome上遇到了这个问题。将我的redux版本从4.0.2降级到3.7.2,为我解决了这个问题
npm卸载redux
npm安装redux@3.7.2
tl;博士
确保你没有任何代码,导致副作用,在你的还原
纯还原剂
Redux还原剂必须是纯的。这意味着它们不应该有副作用。副作用应该是恶作剧或传奇故事。在我的例子中,减速器如下所示:
case REDIRECT_ON_EVENT: {
history.push('/some-route'); // side effect: redirection
return {
...state,
path: action.payload.path,
};
}
history.push('/someroute')代码>部件混乱的状态管理。将其从减速器中移除,并将其放置到在相同动作类型上调用的传奇中,修复了该问题:
function redirectToSomeRoute() {
history.push('/some-route');
}
takeEvery(REDIRECT_ON_EVENT, redirectToSomeRoute),
注意:我使用的是传奇故事
在我的例子中,我在减速器中添加了导航代码
sendOTPSuccess:(状态、操作)=>{
state.wip=false;
RootNavigation.navigate('LoginOTPScreen');
},
开始时,它工作正常,但当我使用/访问被调用的新屏幕中的状态时
从“react-redux”导入{useDispatch,useSelector};
导入*作为来自的会话操作
“Error: You may not call store.getState() while the reducer is executing.”