使用react router和webpack

时间:2016-03-13 11:58:49

标签: webpack react-router redux

我正在使用react,redux和路由器等来构建和示例应用程序。

我正在尝试异步加载应用程序的不同部分。我把我的应用程序分成了鸭子,我正在按照这个例子https://github.com/insin/react-examples/tree/master/code-splitting-redux-reducers

我正在使用:
react-router 2.0.1 webpack 1.12.13 webpack-dev-middleware 1.5.1 webpack-hot-middleware 2.6.4

我的路线:

import App from './components/layouts/app'
import Home from './components/common/home'
import Login from './containers/login'
import Register from './containers/register'

export default function configureRoutes(reducerRegistry) {
  return(
    <Route component={App}>
      <Route  path='/' component={Home} />
      <Route path='/login' component={Login}/>
      <Route path='/register' component={Register}/>
      <Route path='/admin' getComponent={(location, cb) => {
        require.ensure([], require => {
          reducerRegistry.register({admin: require('./modules/admin')})
          if (process.env.NODE_ENV !== 'production') {
            if (module.hot) {
              module.hot.accept('./modules/admin', () => {
                reducerRegistry.register({admin: require('./modules/admin')})
              })
            }
          }
          cb(null, require('./containers/admin'))
        })
      }}/>
    </Route>
)}

我的管理组件:

import React, { PropTypes, Component } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { load } from '../modules/admin'


class Admin extends Component {
  componentDidMount() {
    this.props.load()  
  }
  render() {
    const { message, isFetching } = this.props
    return (
      <div>
        <p>{message}</p> 
        <p>This module was loaded via chunk </p>
        {isFetching && <p>Doing some fake loading ...</p>}
      </div>
    )  
  }  
}

Admin.propTypes = {
  message: PropTypes.string.isRequired,
  isFetching: PropTypes.bool.isRequired,
  load: PropTypes.string.isRequired
}

function mapStateToProps(state) {
  return state.admin 
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({ load }, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(Admin)

问题是/adminthis.props.children组件的app未定义,因此我的组件永远不会被渲染。

我可以在转到/admin后检查状态是否包含管理员。

我可以看到请求和响应请求的块,它确实包含reducer和组件。但他们没有得到应用。

你有什么想法从哪里开始寻找?我不在他们身边

提前致谢

1 个答案:

答案 0 :(得分:4)

我使用ES6模块进行导入和导出,但webpack使用ES5,commonJS模块,因此require请求使用包含default的对象和所有其他属性I&#39;从该模块导出。

所以我必须将require('./x')请求更改为require('./x').default以使其正常工作

export default function configureRoutes(reducerRegistry) {
  return(
    <Route component={App}>
      <Route  path='/' component={Home} />
      <Route path='/login' component={Login}/>
      <Route path='/register' component={Register}/>
      <Route path='/admin' getComponent={(location, cb) => {
        require.ensure([], require => {
          const reducer = require('./modules/admin').default
          const component = require('./containers/admin').default
          reducerRegistry.register({admin: reducer})
          if (process.env.NODE_ENV !== 'production') {
            if (module.hot) {
              module.hot.accept('./modules/admin', () => {
                reducerRegistry.register({admin: reducer})
              })
            }
          }
          cb(null, component)
        })
      }}/>
    </Route>
)}