Search code examples
reactjsreduxreact-reduxreact-router-domconnected-react-router

Routing with react-router-dom, redux, apollo-client, connected-react-router and redux-persist


I am trying to configure an application and I am using: react class components, redux, react-redux, apollo-client, redux-persist and connected-react-redux and I am receiving the following error: "Uncaught TypeError: store.dispatch is not a function".
This is root reducer:

import { combineReducers } from "redux";
import { connectRouter } from "connected-react-router";

const createRootReducer = (history) => combineReducers({
    rooter: connectRouter(history),
    other reducers
})
export default createRootReducer

This is store.js:

import {createStore, applyMiddleware, compose} from 'redux'
import thunk from 'redux-thunk'
import createRootReducer from './Reducers'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
import { createBrowserHistory } from 'history'
import { routerMiddleware } from 'connected-react-router'

const initialState = {}

export const history = createBrowserHistory()

const middware = [thunk]

const persistConfig = { 
    key: 'root',
    storage,     
}
const rootReducer = createRootReducer(history)


const persistedReducer = persistReducer(persistConfig, rootReducer)

const store = createStore(persistedReducer,
    initialState,
    compose(routerMiddleware(history),applyMiddleware(...middware))
    )

const  persistor = persistStore(store)    

export {store, persistor}

And this is index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import {
  ApolloClient,
  InMemoryCache,
  ApolloProvider,
} from "@apollo/client";
import { HashRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import  {store, persistor, history} from './store';
import { PersistGate } from 'redux-persist/integration/react'
import { ConnectedRouter } from 'connected-react-router';

export const client = new ApolloClient({
  uri: ' http://localhost:4000/',
  cache: new InMemoryCache()
});

ReactDOM.render(
  <React.StrictMode>

    <ApolloProvider client={client}>
       <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
         
          <ConnectedRouter history={history}>
            <App />
          </ConnectedRouter>        
      
      </PersistGate></Provider >
    </ApolloProvider>

  </React.StrictMode>,
  document.getElementById('root')
);


React-router-dom version: 5.3.0. React-router version : 5.2.1
Could you please help me?


Solution

  • The problem was with selector, because it is using some cache memory to memoize, and at first load, there was no cache memory. I found this post.
    So I deleted the selector, and moved the function inside the reducer, for which was the selector.
    Also I am not using "connected-react-router" anymore