首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法热更新模块

无法热更新模块
EN

Stack Overflow用户
提问于 2016-07-22 22:24:30
回答 1查看 228关注 0票数 0

我正在尝试让热重载在我的React应用程序中工作,但我不确定如何调试这个错误。

我的应用程序索引如下所示:

代码语言:javascript
复制
import React from 'react'
import ReactDOM from 'react-dom'
import Routes from 'routes/index'
import { browserHistory } from 'react-router'
import syncHistoryWithStore from 'modules/routing/sync'

function createStore (browserHistory) {
  let factory = require('./modules/store')

  const store = factory.createStore(browserHistory)

  if (module.hot) {
    module.hot.accept('./modules/store', () => {
      const nextFactory = require('./modules/store')
      nextFactory.hydrateStore(store, factory.dehydrateStore(store))
      factory = nextFactory
    })
  }

  return store
}

const store = createStore(browserHistory)
const history = syncHistoryWithStore(browserHistory, store, {
  selectLocationState: state => state.routing,
  adjustUrlOnReplay: true
})

ReactDOM.render(
  <Routes history={history} store={store} />,
  document.getElementById('root')
)

当我试图修改我的任何react组件时,就会出现错误。这些更新是通过routes/index路径进行的。但是我不明白为什么我使用的ides的react-redux-universal-hot-example可以工作,而我的却不行?

我可以看到在热示例中,只有'reducer‘路径有一个hot.accept,所以我不太理解got重载react组件是如何工作的,但它们就是这样做的……

对如何调试有什么建议或想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-22 22:35:21

如果我把它和react-hot-loader结合起来,它就能工作:

代码语言:javascript
复制
import { AppContainer } from 'react-hot-loader'
import React from 'react'
import ReactDOM from 'react-dom'
import Routes from 'routes/index'
import { browserHistory } from 'react-router'
import syncHistoryWithStore from 'modules/routing/sync'

function createStore (browserHistory) {
  let factory = require('./modules/store')

  const store = factory.createStore(browserHistory)

  if (module.hot) {
    module.hot.accept('./modules/store', () => {
      const nextFactory = require('./modules/store')
      nextFactory.hydrateStore(store, factory.dehydrateStore(store))
      factory = nextFactory
    })
  }

  return store
}

const store = createStore(browserHistory)
const history = syncHistoryWithStore(browserHistory, store, {
  selectLocationState: state => state.routing,
  adjustUrlOnReplay: true
})

ReactDOM.render(
  <AppContainer>
    <Routes history={history} store={store} />
  </AppContainer>,
  document.getElementById('root')
)

if (module.hot) {
  module.hot.accept('./routes/index', () => {
    // If you use Webpack 2 in ES modules mode, you can
    // use <App /> here rather than require() a <NextApp />.
    const NextRoutes = require('./routes/index').default
    ReactDOM.render(
      <AppContainer>
        <NextRoutes history={history} store={store} />
      </AppContainer>,
      document.getElementById('root')
    )
  })
}

但是,我仍然不明白热重新加载示例是如何工作的?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38528897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档