自动刷新页面(与Ruby On Rails一起反应)

时间:2018-10-15 22:35:21

标签: ruby-on-rails ruby reactjs

client folder where the react code sits我正在使用ruby on rails,我已经创建了一个单独的名为client的文件夹,用于在根目录上放置react代码。但是每当我在react中进行某些更改时,我都必须转到页面并手动刷新它,有什么方法可以在我可以对代码进行任何更改时自动刷新页面吗?

1 个答案:

答案 0 :(得分:0)

如果您正在使用create-react-app,则应该可以通过该软件包使用它。如果要使用Webpack构建自己的环境,则可以使用“热模块替换”。 https://webpack.js.org/guides/hot-module-replacement/

编辑:

这是我在一个项目中包含相关代码的一些示例文件。

Webpack

const webpack = require('webpack');
const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');


module.exports = {

  entry: [
    'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    resolve(__dirname, "src") + "/index.jsx"
  ],

  output: {
    filename: 'app.bundle.js',
    path: resolve(__dirname, 'build'),
    publicPath: '/'
  },

  resolve: {
    extensions: ['.js', '.jsx']
  },

  devtool: '#source-map',

  devServer: {
    hot: true,
    contentBase: resolve(__dirname, 'build'),
    publicPath: '/'
  },

  module: {
    rules: [
      {
        test: /\.jsx?$/,
        enforce: "pre",
        loader: "eslint-loader",
        exclude: /node_modules/,
        options: {
          emitWarning: true,
          configFile: "./.eslintrc.json"
          }
        },
        {
      test: /\.(png|gif|jp(e*)g|svg)$/,
      use: {
        loader: 'url-loader',
        options: {
          limit: 8000,
          name: 'images/[hash]-[name].[ext]'
        }
      }
    },
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          presets: [
            ["es2015", {"modules": false}],
            "react"
          ],
          plugins: [
            "react-hot-loader/babel",
            "styled-jsx/babel"
          ]
        }
      },
    ],
  },

  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(),
    new HtmlWebpackPlugin({
      template:'template.ejs',
      appMountId: 'react-app-root',
      title: 'Project Name',
      filename: resolve(__dirname, "build", "index.html"),
    }),
  ]
};

主要,底部的 entry devServer module.plugins plugins 代码。

package.json

{
  "name": "project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server",
    "lint": "eslint src/index.jsx src/**/*.jsx; exit 0",
    "lint-fix": "eslint src/index.jsx src/**/*.jsx --fix; exit 0"
  },
  "repository": {
    "type": "git",
    "url": "url"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "url"
  },
  "homepage": "relevant-url",
  "dependencies": {
    "eslint": "^5.0.1",
    "eslint-plugin-react": "^7.10.0",
    "file-loader": "^1.1.6",
    "npm": "^6.1.0",
    "prop-types": "^15.5.10",
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-router-dom": "^4.0.0",
    "styled-jsx": "^2.2.7",
    "url-loader": "^0.6.2"
  },
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "eslint-loader": "^2.0.0",
    "html-webpack-plugin": "^2.29.0",
    "react-hot-loader": "^3.0.0-beta.7",
    "webpack": "^3.4.0",
    "webpack-dev-server": "^2.5.0"
  }
}

devDependencies

中的热重装软件包

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
import { AppContainer } from 'react-hot-loader';
import { HashRouter } from 'react-router-dom';

const render = (Component) => {
  ReactDOM.render(
    <AppContainer>
      <HashRouter>
        <Component/>
      </HashRouter>  
    </AppContainer>,
    document.getElementById('react-app-root')
  );
};

render(App);

/*eslint-disable */
if (module.hot) {
  module.hot.accept('./components/App', () => {
    render(App);
  });
}
/*eslint-enable */

请注意从react-hot-loader导入的 AppContainer 组件,该组件将您的应用程序包装在 ReactDOM.render中。。此外,有时 eslint 可能会干扰进行热重装,这就是为什么先禁用它,然后在文件底部再次启用它的原因。

我希望这会有所帮助。