react-router-dom使用create-react-app进行热重载

时间:2018-10-29 01:32:13

标签: javascript reactjs react-router

我正在学习React和React Router。我使用create-react-app创建了一个应用程序,并且热加载效果很好,直到我开始使用react-router-dom

这是我的package.json

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "bulma": "^0.7.2",
    "react": "^16.6.0",
    "react-dom": "^16.6.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.0.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './components/App/App';

const render = () => {
    ReactDOM.render(
        <Router>
            <App />
        </Router>,
        document.getElementById('root')
    );
};

render();

if (module.hot) {
    module.hot.accept('./components/App/App', () => {
        render();
    });
}

App.js

import ...

const Home = () => (
  <div>
    <p className="is-size-1">Home</p>
  </div>
);

class App extends Component {
  render() {
    return (
      <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/guests" exact component={TodaysGuests} />
      </Switch>
    );
  }
}

export default App;

将我的应用封装在BrowserRouter组件中后,热重载不再起作用。我必须重新启动服务器才能看到任何更改。刷新页面无效。

可以热装吗?是否需要使用类似react-hot-loader的东西?

4 个答案:

答案 0 :(得分:0)

尝试使用以下方法更新index.js:

import ...

ReactDOM.render(
  <Router>
    <App />
  </Router>,
  document.getElementById("root")
)
if (module.hot) {
  module.hot.accept("./App", () => {
    ReactDOM.render(<Router><App /></Router>, document.getElementById("root"))
  })
}

答案 1 :(得分:0)

您可能需要重新导入App组件以加载最近的更改。

尝试将您的index.js更改为此:

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './components/App/App';

const render = (Component) => {
  ReactDOM.render(
    <Router>
        <Component />
    </Router>,
    document.getElementById('root')
  );
};

if (module.hot) {
  module.hot.accept('./components/App/App', () => {
    render(require('./components/App/App').default); // This will make sure the App Component is up to date.
  });
}

render(App);

此外,您的App.js需要标记为hot-exported。像这样导出它:

import { hot } from 'react-hot-loader';
...
...
export default hot(module)(App);

答案 2 :(得分:0)

这是我整理的当前工作样板(下面的设置可能与create-react-app不兼容,因此,您可能希望远离它)……

Webpack Hot Boilerplate(自述文件中提到了一些缺点)

.babelrc

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "plugins": [
    "@babel/plugin-transform-runtime",
    "react-hot-loader/babel",

    // Stage 2 https://github.com/babel/babel/tree/master/packages/babel-preset-stage-2
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    "@babel/plugin-proposal-function-sent",
    "@babel/plugin-proposal-export-namespace-from",
    "@babel/plugin-proposal-numeric-separator",
    "@babel/plugin-proposal-throw-expressions",

    // Stage 3
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-syntax-import-meta",
    ["@babel/plugin-proposal-class-properties", { "loose": true }],
    "@babel/plugin-proposal-json-strings"
  ]
}

src / index.js

import React from 'react';
import { render } from 'react-dom';
import App from './root';
import './styles/globals/globals.scss';

const renderApp = () => render(<App />, document.getElementById('root'));

renderApp();

if (module.hot) {
  module.hot.accept('./root', () => renderApp());
}

src / root / index.js

import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import Routes from '../routes';

const App = () => (
  <BrowserRouter>
    <Routes />
  </BrowserRouter>
);

export default App;

src / routes / index.js

import React from 'react';
import { Route, Switch } from 'react-router-dom';
import { hot } from 'react-hot-loader';

import Home from '../components/Home/Home';
import NotFound from '../components/NotFound/NotFound';
import ActiveSession from '../components/ActiveSession/activeSession';

const Routes = () => (
  <div>
    <Switch>
      <Route exact path="/" component={Home} />
      <Route component={NotFound} />
    </Switch>
    <ActiveSession />
  </div>
);

export default hot(module)(Routes);

答案 3 :(得分:-1)

我认为您还有一些其他配置会使热重装无法正常工作。我在项目中使用了您的代码。它仍然可以正常工作。您无需使用react-hot-reloader进行任何操作。只需再次检查您的代码