【问题标题】:React Redux router errorReact Redux 路由器错误
【发布时间】:2016-11-30 08:17:42
【问题描述】:

我正在尝试设置 Redux + React Router 应用程序。我认为问题出在 ImmutableJS,但我不明白如何解决。

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();

我收到此错误:

未捕获的类型错误:state.get 不是函数

state这个对象中

我使用"react": "^15.3.2""redux": "^3.6.0""react-router": "^3.0.0"

更新 1 我现在使用来自redux-immutablecombineReducers

import {combineReducers} from 'redux-immutable'

但是得到一个错误:

未捕获的类型错误:routingState.equals 不是函数

这里:

更新 2

我修复了上述问题,但还有一个错误

我在this存储库中发布的所有代码

【问题讨论】:

    标签: javascript reactjs redux react-router


    【解决方案1】:

    问题停留在src/index.js 文件中,带有require route.js 语句。

    当你 require es6 模块具有 default 时,你必须使用来自 required 模块的默认值。比如,

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

    这解决了您的问题,而您的 git 存储库没有任何其他更改。

    【讨论】:

      【解决方案2】:

      您使用的combineReducers 没有使用immutable。通过设置fromJS(blah),每个分支都是immutable,但不是您所在州的最高级别。请改用redux-immutable

      import {combineReducers} from 'redux-immutable';

      【讨论】:

      • 是的,问题解决了,但没有报错 Uncaught TypeError: routingState.equals is not a function(...)
      • 您要么没有创建历史记录,要么没有将历史记录传递给您的路由器。
      • 我不明白,我的代码应该是什么样子?
      猜你喜欢
      • 2018-02-14
      • 2018-07-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      相关资源
      最近更新 更多