我的index.tsx上有这个错误。
属性'REDUX_DEVTOOLS_EXTENSION_COMPOSE‘不存在于“窗口”类型上。
以下是我的index.tsx代码:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './App';
import './index.css';
import registerServiceWorker from './registerServiceWorker';
import { Provider } from 'react-redux';
import { createStore, compose, applyMiddleware } from 'redux';
import rootReducer from './store/reducers';
import thunk from 'redux-thunk';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(rootReducer, composeEnhancers(
applyMiddleware(thunk)
));
ReactDOM.render( <Provider store={store}><App /></Provider>, document.getElementById('root'));
registerServiceWorker();
我安装了@types/npm安装--保存-dev redux-devtools-扩展,我使用的是创建-反应-应用程序类型记录。非常感谢你对事先发生的一切提出建议。
发布于 2021-07-01 14:37:11
这就是如何在类型记录react应用程序中使用redux-dev-tools
的方法。
为Window
对象创建全局接口:
declare global {
interface Window {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
}
}
然后,按照以下方式创建composeEnhancers
:
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
然后创建一个store
。
const store = createStore(rootReducers, composeEnhancers());
rootReducers
--在我的例子中,指的是在代理文件中创建的combinedReducers
。
现在,您可以像往常一样在Provider
中使用React.js
如下:
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById("root")
);
index.tsx
中的所有代码
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import rootReducers from "./reducers";
import { Provider } from "react-redux";
import { createStore, compose, applyMiddleware } from "redux";
declare global {
interface Window {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
}
}
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(rootReducers, composeEnhancers());
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById("root")
);
reportWebVitals();
发布于 2018-10-14 09:05:24
这是这个问题的一个特例。Redux不为__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
提供类型,因为这个函数是由Redux DevTools公开的,而不是Redux本身。
要么是:
const composeEnhancers = window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] as typeof compose || compose;
或者:
declare global {
interface Window {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
}
}
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
这已经由包含redux-devtools-extension
类型的TypeScript包完成。如果安装了它,则应该使用它的导入,而不是手动访问__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
。
发布于 2019-09-05 02:51:28
使用TypeScript
最简化的方法是使用redux-devtools-扩展并将其安装为一个dev依赖项,如下所示:
npm install --save-dev redux-devtools-extension
对于那些新加入redux
和这些开发工具的人来说,下一步是混乱和不明确的。这些文档都有如下代码:
const store = createStore(reducer, composeWithDevTools(
applyMiddleware(...middleware),
// other store enhancers if any
));
问题是我没有配置任何中间件,所以这是行不通的。在它最原始的用法中,这是您所需要的:
import { composeWithDevTools } from 'redux-devtools-extension';
const store = createStore(myReducer, composeWithDevTools());
此时,如果单击浏览器中的扩展,并且有一个有效的redux存储,则可以检查状态。
这是使用(window as any)
的另一种方法,也明确了如何使用最小形式的redux-devtools-extension
。
https://stackoverflow.com/questions/52800877
复制相似问题