Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types problem whit connected-react-router #416

Open
11ume opened this issue Apr 15, 2020 · 7 comments
Open

Types problem whit connected-react-router #416

11ume opened this issue Apr 15, 2020 · 7 comments

Comments

@11ume
Copy link

11ume commented Apr 15, 2020

The types do not work with react router connect, because the router reducer, returns a different data type, what is the best way to resolve this conflict?

import { createBrowserHistory, History } from 'history'
import { connectRouter } from 'connected-react-router'
import { configureStore } from '@reduxjs/toolkit'

const rootReducer = (history: History) => ({
    router: connectRouter(history)
})

const history = createBrowserHistory()

const store = configureStore({
    reducer: rootReducer(history) // type error
})

export default store
`

> error emited
Types of parameters 'action' and 'action' are incompatible.
Type 'AnyAction' is not assignable to type 'LocationChangeAction<PoorMansUnknown>'.
@11ume 11ume changed the title Typescript typings not working whit connected-react-router Types problem whit connected-react-router Apr 15, 2020
@lkostrowski
Copy link

Same here, I'm trying to solve it for a long time but didn't figure out anything :/

Image 2020-04-17 at 4 09 27 PM

@11ume
Copy link
Author

11ume commented Apr 17, 2020

@lkostrowski Hi, I solved this problem, but not elegantly, it was a recommendation from the creator of redux-toolkit.
Another solution might be to clone the connected-react-router type repository and solve the problem the right way, but I don't have time for make it.

My provisional solution is the next.
Note: I am using redux-toolkit.

import { Reducer } from 'redux'
import { History, LocationState } from 'history'
import { connectRouter, RouterState } from 'connected-react-router'
import counter from 'features/couter'

const createRootReducer = (history: History) => ({
    router: connectRouter(history) as Reducer<RouterState<LocationState>>
    , counter
})

export default createRootReducer
`

@lkostrowski
Copy link

Thanks, it works. Its not that bad if you keep this hack in one place.
It still cause some problems with typing actions, but nothing I can't live with.

@matinrco
Copy link

I know it's a little late, but for those who end up here:

In latest version of connected-react-router, reducer type problem is solved (#431 I think), but I had problems while dispatching thunk actions with broken types caused by incorrect connected-react-router middleware setup with toolkit.
I used cra-template-redux-typescript and this is what I did:

import { configureStore } from "@reduxjs/toolkit";
import { combineReducers } from "redux";
import { routerMiddleware, connectRouter } from "connected-react-router";
import { createBrowserHistory } from "history";
....

const browserHistory= createBrowserHistory()

const rootReducer = combineReducers({
    router: connectRouter(browserHistory),
    counter: counterReducer,
});

const store = configureStore({
    reducer: rootReducer,
    middleware: (getDefaultMiddleware) => [
        ...getDefaultMiddleware(),
        routerMiddleware(browserHistory),
    ],
});

the point is that to use callback function with getDefaultMiddleware instead of array to setup middlewares according to docs here, and now everything works perfectly 👌

@leosuncin
Copy link

I've found an error with strict: true:

import { Action, ThunkAction, configureStore } from '@reduxjs/toolkit';

import { connectRouter, routerMiddleware } from 'connected-react-router';
import { History, createBrowserHistory } from 'history';

import authReducer from './slices/authSlice';
import cartReducer from './slices/cartSlice';

export const browserHistory = createBrowserHistory();

export function makeStore(history: History = browserHistory) {
  return configureStore({
    reducer: {
      cart: cartReducer,
      auth: authReducer,
      router: connectRouter(history),
    },
    middleware: (getDefaultMiddleware) => [
      ...getDefaultMiddleware(),
      routerMiddleware(history),
    ],
  });
}

const store = makeStore();


export type AppState = ReturnType<typeof store.getState>;

export type AppDispatch = typeof store.dispatch;

export type AppThunk<ReturnType = void> = ThunkAction<
  ReturnType,
  AppState,
  unknown,
  Action<string>
>;

export default store;

I got the next error

src/app/store.ts:19:7 - error TS2322: Type 'Reducer, AnyAction>' is not assignable to type 'Reducer'.
  Types of parameters 'state' and 'state' are incompatible.
    Type 'unknown' is not assignable to type 'RouterState | undefined'.
      Type 'unknown' is not assignable to type 'RouterState'.

19       router: connectRouter(history),
         ~~~~~~


Found 1 error.

With [email protected] and @reduxjs/[email protected]

@ksjogo
Copy link

ksjogo commented Jun 22, 2021

Also ran into that with strict turned on.

@JacekKosciesza
Copy link

JacekKosciesza commented Nov 2, 2021

For me something like that worked:

router: connectRouter<LocationState>(browserHistory)

full example:

import { configureStore } from "@reduxjs/toolkit";
import { connectRouter, routerMiddleware } from "connected-react-router";
import { createBrowserHistory, LocationState } from "history";

export const browserHistory = createBrowserHistory();

export const store = configureStore({
  reducer: {
    router: connectRouter<LocationState>(browserHistory),
    // ...
  },
  middleware: (getDefaultMiddleware) => [
    ...getDefaultMiddleware(),
    routerMiddleware(browserHistory),
  ],
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants