我正在使用https://github.com/stylesuxx/generator-react-webpack-redux作为生成器而我正在使用hot-loader。我的代码能够在浏览器中正常加载,但是当我在浏览器中打开开发面板时,我遇到了
React Hot Loader: this component is not accepted by Hot Loader.
Please check is it extracted as a top level class, a function or a variable.
Click below to reveal the source location:
ƒ (props, context, updater) {
// This constructor gets overridden by mocks. The argument is used
// by mocks to assert on what gets mounted.
if (process.env.NODE_ENV !== 'production'…
在我的client.js
中,代码如下所示:
import React from 'react';
import ReactDOM from 'react-dom';
import { AppContainer } from 'react-hot-loader';
import { Provider } from 'react-redux';
import { Router, IndexRoute, Route, browserHistory, hashHistory } from 'react-router';
import App from './containers/App';
import configureStore from './stores';
import Contact from './containers/contact/Contact'
import Homepage from './containers/homepage/Homepage'
import About from './containers/about/About'
import Theme from './containers/themes/Themes'
import Login from './containers/login/Login'
import Signup from './containers/signup/Signup'
const store = configureStore();
const routes = {
path: '/',
indexRoute: {onEnter: (nextState, replace) => replace('/home')},
childRoutes: [
require('./containers/homepage').default,
require('./containers/themes').default,
require('./containers/contact').default,
require('./containers/about').default,
require('./containers/login').default,
require('./containers/signup').default,
{
path: '*',
indexRoute: { onEnter: (nextState, replace) => replace('/error/404') }
}
]
}
ReactDOM.render((
<Provider store={store}>
<Router onUpdate = {() => window.scrollTo(0, 0)}
history={hashHistory}
routes={routes}
/>
</Provider>
), document.getElementById('app'));
// if (module.hot) {
// module.hot.accept('./containers/App', () => {
// const NextApp = require('./containers/App').default; // eslint-
disable-line global-require
//
// ReactDOM.render(
// <AppContainer>
// <Provider store={store}>
// <NextApp />
// </Provider>
// </AppContainer>,
// document.getElementById('app')
// );
// });
// }
我应该如何修改注释代码以使Hot Loader接受组件,我尝试使用下面的代码并且它不起作用。
if (module.hot) {
module.hot.accept('./containers/App', () => {
const NextApp = require('./containers/App').default; // eslint-disable-line global-require
ReactDOM.render(
<AppContainer>
<Provider store={store}>
<Router onUpdate = {() => window.scrollTo(0, 0)}
history={hashHistory}
routes={routes}
/>
</Provider>
</AppContainer>,
document.getElementById('app')
);
});
}
答案 0 :(得分:0)
我看到很多代码重复。而不是所有ReactDOM.render((
尝试创建为您呈现DOM的函数。
查看我的示例repo
首先我创建render
函数:
const render = () => { ReactDOM.render(
(
<Provider store={store}>
<BrowserRouter>
{renderRoutes(routes)}
</BrowserRouter>
</Provider>
),
document.getElementById('root'), ) }
然后我渲染应用程序:
render()
然后我处理热重装:
if (module.hot) {
module.hot.accept()
}
也可以像这样处理:
if (module.hot) {
module.hot.accept('./app', () => {
const UpdatedApp = require('./app').default // require('./app').default = renderRoutes(routes)
render(UpdatedApp)
})
}
但我的观点是你不想复制渲染DOM代码。当您提取代码并创建用于呈现客户端代码的专用函数时。您会看到解决方案更加清晰。
它还可以帮助您处理服务器端呈现,因为您将能够使用相同的功能在服务器上呈现代码。