Pavel Perevezencev
Pavel Perevezencev

Reputation: 2978

React Redux router error

I'm trying setup a Redux + React Router app. I think the problem is with ImmutableJS, but I do not understand how to resolve it.

client.js

import { fromJS } from 'immutable'
import React from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
import { AppContainer } from 'react-hot-loader'

import { Provider } from 'react-redux'
import { match, Router, browserHistory } from 'react-router'
import { syncHistoryWithStore } from 'react-router-redux'

import { createStore, combineReducers, compose, applyMiddleware } from 'redux'
import { routerReducer, routerMiddleware } from 'react-router-redux'

function createSelectLocationState(reducerName) {
  let prevRoutingState;
  let prevRoutingStateJS;

  return (state) => {
    const routingState = state.get(reducerName); // or state.routing

    if (!routingState.equals(prevRoutingState)) {
      prevRoutingState = routingState;
      prevRoutingStateJS = routingState.toJS();
    }

    return prevRoutingStateJS;
  };
}

function configureStore(history, initialState) {
  const reducer = combineReducers({
    routing: routerReducer
  });

  return createStore(
    reducer,
    initialState,
    compose(
      applyMiddleware(
        routerMiddleware(history)
      )
    )
  );
}

const initialState = fromJS(window.__INITIAL_STATE__);
const store = configureStore(browserHistory, initialState);
const history = syncHistoryWithStore(browserHistory, store, {
  selectLocationState: createSelectLocationState('routing')
});
const rootNode = document.getElementById('root');

const renderApp = () => {
  const routes = require('./routes');

  match({ history, routes }, (error, redirectLocation, renderProps) => {
    render(
      <AppContainer>
        <Provider store={store}>
          <Router {...renderProps} />
        </Provider>
      </AppContainer>,
      rootNode
    );
  });
};

// Enable hot reload by react-hot-loader
if (module.hot) {
  const reRenderApp = () => {
    try {
      renderApp();
    } catch (error) {
      const RedBox = require('redbox-react').default;

      render(<RedBox error={error} />, rootNode);
    }
  };

  module.hot.accept('./routes', () => {
    setImmediate(() => {
      // Preventing the hot reloading error from react-router
      unmountComponentAtNode(rootNode);
      reRenderApp();
    });
  });
}

renderApp();

I get this error:

Uncaught TypeError: state.get is not a function

enter image description here

In state this object

enter image description here

I use "react": "^15.3.2", "redux": "^3.6.0", "react-router": "^3.0.0"

UPDATE 1 I now use combineReducers from redux-immutable:

import {combineReducers} from 'redux-immutable'

But get an error:

Uncaught TypeError: routingState.equals is not a function

enter image description here

Here:

enter image description here

UPDATE 2

I fixed hereinabove issue, but there one more error

enter image description here

All code i posted in this repository

Upvotes: 5

Views: 659

Answers (2)

Thaadikkaaran
Thaadikkaaran

Reputation: 5226

The problem stays at src/index.js file with the require statement of route.js.

When you require es6 module which has default, you have to use the default from required module. Something like,

const routes = require('./routes').default;

This fixed your issues without any other change on your git repo.

Upvotes: 1

Johnny Klironomos
Johnny Klironomos

Reputation: 214

The combineReducers you are using is not using immutable. Each branch is immutable by setting fromJS(blah) but not on the highest level of your state. Use redux-immutable instead:

import {combineReducers} from 'redux-immutable';

Upvotes: 0

Related Questions