【问题标题】:Browser page doesn't auto-refreshes in react project浏览器页面不会在反应项目中自动刷新
【发布时间】:2021-09-21 16:02:12
【问题描述】:

import React from "react";
import "./App.css";

function SecretComponent() {
  return <p>Secret2 information for authorised users only </p>;
}

function RegularComponent() {
  return <p>Everyone1 can see this component</p>;
}

function App(props) {
  if (props.authorized) {
    return <SecretComponent />;
  } else {
    return <RegularComponent />;
  }
}

import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";

ReactDOM.render(<App authorized={false} />, document.getElementById("root"));

在我在 Visual Studio Code 中的 react 项目中,当我进行更改时,浏览器页面仅在 App.js 文件中进行更改时刷新,但在 index.js 文件中进行更改时不会刷新。我必须手动刷新页面才能显示更改。

【问题讨论】:

  • 你能分享一个回购例子吗?
  • 你在用create-react-app吗?
  • npx create-react-app &lt;app-name&gt; 我用npm start运行它
  • 当我更改 authorized={true} 中的值时,我注意到此更改不会在 Bowser 开发组件中传播。授权仍然显示authorised: false,直到我刷新浏览器。页面刷新后,值与代码库同步。

标签: javascript reactjs visual-studio-code


【解决方案1】:

试试这个:

  1. npm install react-hot-loader

  2. app.js 中,添加 import { hot } from 'react-hot-loader/root'

  3. 像这样在导出export default hot(App) 时包装App 组件。

【讨论】:

猜你喜欢
  • 2020-12-02
  • 2021-09-19
  • 1970-01-01
  • 2021-01-13
  • 2013-09-08
  • 2020-08-12
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多