简体   繁体   English

反应URL更改,但组件状态相同

[英]React url changes but component state the same

I've needed to go to "other" web-page but when i click on the my "Link", that wraps the button, the URL changes but web-page the same 我需要转到“其他”网页,但是当我单击“我的链接”时,它包装了按钮,URL发生了变化,但网页相同

In App.js : App.js

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

class App extends Component {
    render() {
        return (
            <>
                <Switch>
                    <Route exact path="/" component={Home}/>
                    <Route exact path="/rooms" component={Rooms}/>
                    <Route exact path="/rooms/:room" component={Chat}/>
                    <Route exact component={Error}/>
                </Switch>
            </>
        );

    }
}

that component: 该组件:

import React, {Component} from "react"
import { BrowserRouter as Router, Route, Link } from "react-router-dom";

...
      <Link to="/rooms"  style = {{textDecoration: "none"}}> //That link
        <div class="btn" >  {this.props.Title == "Sing Up" ?"Register Now!" :"Lets Go!"} </div>
      </Link>

I expect the changing of page.enter code here 我希望在这里更改page.enter代码

Try rendering the application inside of a BrowserRouter component. 尝试在BrowserRouter组件内部呈现应用程序。

import { Component } from "react" 
import { BrowserRouter, Switch, Route, Link } from "react-router-dom"

class App extends Component {
    render() {
        return (
            <BrowserRouter>
                <Switch> etc </Switch>
            </BrowserRouter>
        )
    }
}

Also the button is using class instead of className . 同样,该按钮使用的是class而不是className

<div className="btn">

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

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