React Router更改URL,但除非从浏览器刷新页面,否则不会更新页面

时间:2020-06-22 07:42:27

标签: javascript reactjs react-router react-router-dom

因此,我在YouTube上观看了很多针对初学者的关于React的教程, 但是,我使用的是该URL(localhost /),然后单击“设置”按钮将URL更改为(localhost / settings),并且不会在浏览器中自动执行。如果我使用URL(localhost / settings)刷新了浏览器,页面已从Settings.js更新并呈现

这是我的代码和JS(如果您愿意帮助我的话): header.js

class Header extends Component {

    state = {
        MainMenuAppeared:false
    }

    MainMenuShow = () =>{
        this.setState({
            MainMenuAppeared: true
        })
    }

    MainMenuHide = () =>{
        this.setState({
            MainMenuAppeared: false
        })
    }

    render() {
        return (
            <div className="Header">
              <header>
                  {this.state.MainMenuAppeared ? (
                    <div id="logo" onClick={this.MainMenuHide}><WebLogo /></div>
                  ) : (
                    <div id="logo" onClick={this.MainMenuShow}><WebLogo /></div>
                  )}

                {this.state.MainMenuAppeared ?(
                    <Router>
                <div id="MainMenu">
                    <Link to="/"><div className="MenuLists">Home</div></Link>
                    <Link to="/settings"><div className="MenuLists">Settings</div></Link>
                </div></Router>) : (<div></div>)}
              </header>
            </div>
          );
    }
}

Main.js(以前称为App.js)

function Main() {
  return (
    <div className="Main">
      <Header />
      <br></br>
      <br></br>
      <br></br>
      <Router>
        <Switch>
          <Route exact path="/" component={Home} />
          <Route path="/settings" component={Settings}/>
        </Switch>
      </Router>
    </div>
  );
}

Settings.js

function Settings() {
  return (
    <div className="Settings">
     Settings
    </div>
  );
}

2 个答案:

答案 0 :(得分:2)

您的Header组件应位于Router组件内。 工作Codesandbox link

<div className="Main">
  <Router>
    <Header />
    <br />
    <br />
    <br />
    <Switch>
      <Route exact path="/" component={Home} />
      <Route path="/settings" component={Settings} />
    </Switch>
  </Router>
</div>

让我知道它是否有效!

答案 1 :(得分:0)

在组件树的顶部应该有一个Router。因此,只需将Header移到Router

function Main() {
  return (
    <div className="Main">
      <Router>
        <Header />
        <br></br>
        <br></br>
        <br></br>
     
        <Switch>
          <Route path="/settings" component={Settings}/>
          <Route exact path="/" component={Home} />
        </Switch>
      </Router>
    </div>
  );
}

并从Router本身中删除第二个Header

class Header extends Component {
  state = {
    MainMenuAppeared: false
  };

  MainMenuShow = () => {
    this.setState({
      MainMenuAppeared: true
    });
  };

  MainMenuHide = () => {
    this.setState({
      MainMenuAppeared: false
    });
  };

  render() {
    return (
      <div className="Header">
        <header>
          {this.state.MainMenuAppeared ? (
            <div id="logo" onClick={this.MainMenuHide}>
              <WebLogo />
            </div>
          ) : (
            <div id="logo" onClick={this.MainMenuShow}>
              <WebLogo />
            </div>
          )}

          {this.state.MainMenuAppeared && (
           
              <div id="MainMenu">
                <Link to="/">
                  <div className="MenuLists">Home</div>
                </Link>
                <Link to="/settings">
                  <div className="MenuLists">Settings</div>
                </Link>
              </div>
           
          )}
        </header>
      </div>
    );
  }
}