0

I'm trying to build react app with typescript using redux for state management but I'm getting this error on the app component.enter image description here

-app.tsx

import React from 'react';
import { Provider } from 'react-redux';
//import { Router } from "react-router";
import store from './redux/store';
import './App.css';

function App() {
  return (
    <Provider store={store}>
    </Provider>
  );
}

export default App;

-Store.tsx

import { createStore, applyMiddleware, compose } from 'redux';
import { createLogger } from 'redux-logger';
import thunk from 'redux-thunk';

import rootReducer from './reducers';

const loggerMiddleware = createLogger()

// For redux DevTool
declare global {
    interface Window {
      __REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
    }
}
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const enhancer = composeEnhancers(
    applyMiddleware(thunk, loggerMiddleware)
);

const initialState = {};

const store = createStore(rootReducer, initialState, enhancer);

export default store;

I tried to inject the store in the index but i have the same error with.. Any idea where the issues came from??

4
  • hey, did my answer solve your issue? Commented May 21, 2020 at 12:29
  • Not yet I created the type on types.d.tsx but I'm still getting the error.. Commented May 21, 2020 at 16:47
  • You shouldn't put it on types.d.tsx, but rather on a .tsx or .ts file instead. In addition, you will need to use the type on your rootReducer and initialState too. Commented May 21, 2020 at 16:53
  • Okay I see.. thank you man.. Commented May 21, 2020 at 18:01

1 Answer 1

1

That is because TypeScript is unable to infer the typings of your redux store state.

If you have not created the types, you will need to define the types of your store using interfaces or type aliases:

export interface RootState {
  // add required properties from your store
}

On your app.tsx,

import { Store as ReduxStore } from 'redux';
 // other imports 

interface AppProps {
  store: ReduxStore<RootState>;
}

const App: React.FC<AppProps> = ({ store }) => (
  <Provider store={store}>
    <App onLoad={onLoad} />
  </Provider>
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.