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

时间:2019-07-10 17:33:32

标签: javascript reactjs

我需要转到“其他”网页,但是当我单击“我的链接”时,它包装了按钮,URL发生了变化,但网页不变了

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>
            </>
        );

    }
}

该组件:

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>

我希望在这里更改page.enter代码

1 个答案:

答案 0 :(得分:0)

尝试在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>
        )
    }
}

该按钮还使用class而不是className

<div className="btn">