admin管理员组文章数量:1434924
I'm trying to connect Redux Store to my TypeScript-React app, but receiving the following error:
No overload matches this call. Overload 1 of 2, '(props: Readonly>): Provider', gave the following error. Type '() => any' is missing the following properties from type 'Store': dispatch, getState, subscribe, replaceReducer, [Symbol.observable] Overload 2 of 2, '(props: ProviderProps, context?: any): Provider', gave the following error. Type '() => any' is not assignable to type 'Store'.ts(2769) index.d.ts(461, 5): The expected type es from property 'store' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes> & Readonly> & Readonly<...>' index.d.ts(461, 5): The expected type es from property 'store' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes> & Readonly> & Readonly<...>'
here is my index.tsx ponent:
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { ConnectedRouter } from 'connected-react-router';
import { Route, Switch } from 'react-router-dom';
import store from './store/index';
import './styles/index.scss';
import LoginPage from './app/routes/LoginPage';
const App = () => (
<Provider store={store}>
<Switch>
<Route path="/" ponent={LoginPage} />
</Switch>
</Provider>
);
ReactDOM.render(<App />, document.getElementById('root'));
and my store:
store.js
import { createStore } from 'redux';
export default configureStore = () => {
const store = createStore(countReducer);
return store;
};
reducer.js:
const countReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};
Should I specify the type of a store somewhere?
I'm trying to connect Redux Store to my TypeScript-React app, but receiving the following error:
No overload matches this call. Overload 1 of 2, '(props: Readonly>): Provider', gave the following error. Type '() => any' is missing the following properties from type 'Store': dispatch, getState, subscribe, replaceReducer, [Symbol.observable] Overload 2 of 2, '(props: ProviderProps, context?: any): Provider', gave the following error. Type '() => any' is not assignable to type 'Store'.ts(2769) index.d.ts(461, 5): The expected type es from property 'store' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes> & Readonly> & Readonly<...>' index.d.ts(461, 5): The expected type es from property 'store' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes> & Readonly> & Readonly<...>'
here is my index.tsx ponent:
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { ConnectedRouter } from 'connected-react-router';
import { Route, Switch } from 'react-router-dom';
import store from './store/index';
import './styles/index.scss';
import LoginPage from './app/routes/LoginPage';
const App = () => (
<Provider store={store}>
<Switch>
<Route path="/" ponent={LoginPage} />
</Switch>
</Provider>
);
ReactDOM.render(<App />, document.getElementById('root'));
and my store:
store.js
import { createStore } from 'redux';
export default configureStore = () => {
const store = createStore(countReducer);
return store;
};
reducer.js:
const countReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};
Should I specify the type of a store somewhere?
Share Improve this question edited Feb 25, 2020 at 11:20 ROOT 11.6k5 gold badges34 silver badges48 bronze badges asked Feb 25, 2020 at 10:59 KarenKaren 1,4295 gold badges27 silver badges50 bronze badges 2-
From where you are getting this
countReducer
understore.js
? – Mayank Pandeyz Commented Feb 25, 2020 at 11:26 -
You have to pass the reducer or bined reducer under
createStore()
, in your case you are passing it but haven't imported it may be this is the issue – Mayank Pandeyz Commented Feb 25, 2020 at 11:29
1 Answer
Reset to default 4You exported store
as a function, but using it like a variable. Don't forget to invoke the function.
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { ConnectedRouter } from 'connected-react-router';
import { Route, Switch } from 'react-router-dom';
import store from './store/index'; // store is a function
import './styles/index.scss';
import LoginPage from './app/routes/LoginPage';
const App = () => (
<Provider store={store()}> // <- Call the function here
<Switch>
<Route path="/" ponent={LoginPage} />
</Switch>
</Provider>
);
ReactDOM.render(<App />, document.getElementById('root'));
本文标签: javascriptCan39t connect Redux store to React Typescript AppStack Overflow
版权声明:本文标题:javascript - Can't connect Redux store to React Typescript App - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745620025a2666627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论