5

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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';

3 Comments

Yes, problem is solved, but not error Uncaught TypeError: routingState.equals is not a function(…)
You either not creating history or you are not passing history down to your router.
I don't understand, how it should look like in my code?

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.