简体   繁体   English

React-hot-loader不重发组件

[英]React-hot-loader not re-rending components

I have an application that is serving an EJS view with my React.js bundle loaded in a script tag. 我有一个应用程序,该应用程序将我的React.js包加载到脚本标签中,从而为EJS视图提供服务。

I have followed the instructions for react-hot-loader v3 and @next, neither are working. 我已经按照react-hot-loader v3和@next的说明进行了操作,但都无法正常工作。 The are no errors in the console and the components are not being re-rendered on change. 控制台中没有错误,并且更改时不会重新呈现组件。

Here is my app entry point: 这是我的应用程序入口点:

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);
  })
}

Here is the App files that the entry point is calling: 这是入口点正在调用的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)

Here is my webpack dev config: 这是我的webpack开发配置:

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 })
  };

Here are my JavaScript rules: 这是我的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
  };

Developer plugins: 开发人员插件:

  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'
      })
    ];
  }

I'm assuming this is coming from a misunderstanding about how react-hot-loader works, but I cannot find any other information about why it wouldn't work in the docs or examples. 我假设这是由于对react-hot-loader的工作方式有误解,但我在文档或示例中找不到任何其他有关为何它不起作用的信息。

I know the pain of configuring this stuff. 我知道配置这些东西的痛苦。

You need to include the next lines in your entry section 您需要在输入部分中加入下一行

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

then when you are setting babel-loader configuration you need to add react-hot-loader/babel 然后在设置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 I have a simple configuration with this feature. 在这里,我对此功能进行了简单的配置。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM