【问题标题】:React url changes but component state the sameReact url 更改但组件状态相同
【发布时间】:2019-07-11 04:19:34
【问题描述】:

我需要转到“其他”网页,但是当我点击包裹按钮的“链接”时,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>

我希望页面发生变化。在此处输入代码

【问题讨论】:

  • 看起来正确。控制台中是否存在阻止页面呈现的 JavaScript 错误?
  • 没有。它只是更改了一个 URL,但实际上并没有更改页面
  • 是的,我跟着。你能确认你的控制台没有错误吗? developers.google.com/web/tools/chrome-devtools/open
  • 另外,您能否提供将应用安装到页面的顶级代码?
  • 在我的控制台中没有任何错误,但在开发工具中我有一个(但我已经解决了这个问题):

标签: javascript reactjs


【解决方案1】:

尝试在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">

【讨论】:

  • 我写道我已经修复了“类”
  • 我不知道,我在互联网上搜索它应该可以工作
猜你喜欢
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 2018-09-21
  • 2018-08-08
  • 2019-03-05
相关资源
最近更新 更多