React-hot-loader不会重新发送组件

时间:2018-02-15 10:07:02

标签: javascript reactjs express webpack react-hot-loader

我有一个提供EJS视图的应用程序,我的React.js包加载在脚本标记中。

我已经按照react-hot-loader v3和@next的说明进行操作,两者都没有工作。控制台中没有错误,组件在更改时不会重新呈现。

这是我的应用入口点:

import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';

import getMuiTheme from 'material-ui/styles/getMuiTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import injectTapEventPlugin from 'react-tap-event-plugin';
import { AppContainer } from 'react-hot-loader'

import { store } from './routes';

import App from './containers/App';

const render = Component => {
  ReactDOM.render(
    <Provider store={store}>
      <MuiThemeProvider>
        <AppContainer>
          <Component />
        </AppContainer>
      </MuiThemeProvider>
    </Provider>,
    document.getElementById('root'))
};

render(App);
//registerServiceWorker();

// Webpack Hot Module Replacement API
if (module.hot) {
  console.log('module.hot exists');
  module.hot.accept('./containers/App', () => {
    console.log('Reloading app');
    render(App);
  })
}

以下是入口点正在调用的App文件:

import React from 'react';
import Navigation from '../components/Navigation';
import { hot } from 'react-hot-loader'
import createRoutes from '../routes';

const routes = createRoutes();

const App = () => {
  return ([
    <Navigation key="app-navigation" />,
    <main style={{ height: 'calc(100% - 85px)' }} key="app-main">{routes}</main>
  ])
};

export default hot(module)(App)

这是我的webpack dev config:

const devBrowserRender = {
    devtool: 'source-map',
    context: PATHS.app,
    entry: {
      app: ['babel-polyfill', 'react-hot-loader/patch',
        './index']
    },
    node,
    devServer: {
      contentBase: PATHS.assets,
      hot: true
    },
    output: {
      path: PATHS.assets,
      filename: '[name].dev.js',
      publicPath: PATHS.public
    },
    module: { rules: rules({ production: false, browser: true }) },
    resolve,
    plugins: plugins({ production: false })
  };

以下是我的JavaScript规则:

  const presets = [["es2015", { "modules": false }], 'react', 'stage-0'];

  const plugins = production ? [
    'transform-react-remove-prop-types',
    'transform-react-constant-elements',
    'transform-react-inline-elements'
  ] : [];

  return {
    test: /\.js$|\.jsx$/,
    loader: 'babel-loader',
    options: {
      presets,
      plugins
    },
    exclude: PATHS.modules
  };

开发人员插件:

  if (!production) {
    return [
      new webpack.NamedModulesPlugin(),
      new webpack.HotModuleReplacementPlugin(),
      new webpack.EnvironmentPlugin(['NODE_ENV']),
      new webpack.DefinePlugin(compileTimeConstantForMinification),
      new ExtractTextPlugin({
        filename: '[contenthash].css',
        allChunks: true
      }),
      // new webpack.optimize.UglifyJsPlugin({ compress }),
      new ManifestPlugin({
        fileName: 'manifest.json'
      })
    ];
  }

我假设这是因为对反应热门加载器如何工作的误解,但我找不到任何其他信息,说明为什么它不会在文档或示例中起作用。

1 个答案:

答案 0 :(得分:0)

我知道配置这些东西的痛苦。

您需要在输入部分中包含下一行

entry: {
  app: [
    'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:3000',
    'webpack/hot/only-dev-server',
    './index.js'
  ]
}

然后,当您设置babel-loader配置时,您需要添加react-hot-loader/babel

const plugins = production ? [
  'transform-react-remove-prop-types',
  'transform-react-constant-elements',
  'transform-react-inline-elements'
] : ['react-hot-loader/babel'];

Here我有一个使用此功能的简单配置。

希望它有所帮助。