React-router: getChildRoutes not firing in nested

2019-09-10 10:14发布

问题:

For reference, I am using the React-Redux Starter Kit (https://github.com/davezuko/react-redux-starter-kit) as a base project layout.

Ive got a route named TSP, and I am using getComponent and redux connect() to inject a reducer on a container component, which i am hoping will pass children on in the props.

The getChildRoutes in the TSP route definition is never being called.

Root Router Config:

import ApplicationLayout from '../layouts/ApplicationLayout';
import Home from './Home';

export const createRoutes = (store) => {
  const routes = {
    path: '/',
    component: ApplicationLayout,
    indexRoute: Home,
    getChildRoutes (location, next) {
      require.ensure([], (require) => {
        next(null, [
          require('./TSP').default(store),
          require('./Home').default,
        ]);
      });
    }
  };

  return routes;
};

export default createRoutes;

Nested Router Config (TSP):

import { injectReducer } from '../../store/reducers';
import Overview from './routes/Overview';

export default (store) => ({
  path: '/tsp/:id',
  indexRoute: Overview,
  getComponent (nextState, next) {
    require.ensure([
      './containers/TSPContainer',
      './modules/tsp'
    ], (require) => {
      const TSP = require('./containers/TSPContainer').default;
      const reducer = require('./modules/tsp').default;

      injectReducer(store, { key: 'tsp', reducer });
      next(null, TSP);
    });
  },
  getChildRoutes (location, next) {
    debugger
    require.ensure([], (require) => {
      next(null, [
        // Provide store for async reducers and middleware
        require('./routes/Offers').default(store),
        require('./routes/Reviews').default(store)
      ]);
    });
  }
});

I am never getting to the debugger within getChildRoutes.

Any help is appreciated, and if there are any more files necessary to see I can add them.

回答1:

Have you synchronously loaded the ./TSP and ./Home route definitions? If not, I don't think require.ensure would work without you specifying those modules as dependencies.

So:

getChildRoutes (location, next) {
  require.ensure([
    './TSP',
    './Home'
  ], (require) => {
    next(null, [
      require('./TSP').default(store),
      require('./Home').default,
    ]);
  });
}


回答2:

Figured it out. My IndexRoute in the above TSP nested router config was a function, not an object (which i'm assuming react-router was expecting indexRoute to be an object before firing getChildRoutes.)

The solution was to assign the resolved the routeConfig for Overview to indexRoute.

TSP/index.js

import { injectReducer } from '../../store/reducers';

export default (store) => ({
  path: '/tsp/:id',
  indexRoute: require('./routes/Overview').default(store),
  getComponent (nextState, next) {
    require.ensure([
      './containers/TSPContainer',
      './modules/tsp'
    ], (require) => {
      const TSP = require('./containers/TSPContainer').default;
      const reducer = require('./modules/tsp').default;

      injectReducer(store, { key: 'tsp', reducer });
      next(null, TSP);
    });
  },
  getChildRoutes (location, next) {
    require.ensure([], (require) => {
      next(null, [
        // Provide store for async reducers and middleware
        require('./routes/Offers').default(store),
        require('./routes/Reviews').default(store)
      ]);
    });
  }
});

TSP/routes/Overview/index.js

export default (store) => ({
  getComponent (nextState, next) {
    require.ensure([
      './components/OverviewView'
    ], (require) => {
      const Overview = require('./components/OverviewView').default;
      next(null, Overview);
    });
  }
});