【问题标题】:React component path rendering home path tooReact 组件路径也渲染主路径
【发布时间】:2022-01-21 02:12:32
【问题描述】:

我有几个按钮可以使用 useHistory 将路径更改为另一个组件,这适用于所有页面,但在其中两个页面上,作为我的应用程序启动路径的“/”路径也呈现在我尝试的组件下方到达,所以它最终看起来像这样:

我的应用被包裹在一个带有精确路由路径的 BrowserRouter 中,这就是它在我的 index.js 中的样子:

   ReactDOM.render(
  <React.StrictMode>
    <Router>
          <Switch>
            <Route exact strict path="http://localhost:3000/customerloginpage" component={RedirectCustomerLoginComponent}/>
            <Route exact strict path="/adminloginpage" component={AdminLoginPage}/>
            <Route exact strict path="/adminhomepage" component={AdminHomePage}/>
            <Route exact strict path="/listcustomers" component={ListCustomers}/>
            <Route exact strict path="/listcars" exact component={ListCars}/>
            </Switch>
      <App/>
    </Router>
  </React.StrictMode>,
  document.getElementById('root')
);

要访问我的 listcars 组件,我单击 AdminHomePage 组件中的一个按钮,该按钮使用 listcars 组件的 useHistory 路径,这就是我的 AdminHomePage 组件的外观:

function AdminHomePage() {

 const history = useHistory();

 const logoutButtonClicked = () =>{ 
    let path = '/adminloginpage'; 
    history.push(path);
  }

  const listCustomersButtonClicked = () =>{ 
    let path = '/listcustomers'; 
    history.push(path);
  }

  const listCarsButtonClicked = () => { 
    let path = '/listcars'; 
    history.push(path);
  }

    return (
        <div className="App-header">
        <button id="logoutButton" onClick={logoutButtonClicked}>Logout</button>
        <br/>
        <button id="listCustomersButton" onClick={listCustomersButtonClicked}> List Customers</button>   
        <button id="listCarsButton" onClick={listCarsButtonClicked}> List Cars</button>       
        </div>
        
    )

    
}

export default withRouter(AdminHomePage);

我的 listcars 组件看起来像这样:

import React, { Component } from "react";
import CarService from "../services/CarService";
import { withRouter } from "react-router";


class ListCars extends Component{

    constructor(props){
        super(props)
        this.state =  {
            cars:[]
        }
    }

    componentDidMount() {
        CarService.getCars().then((response) => {
            this.setState({cars: response.data})
        })
    }

    render(){
        return (
            <div> 
                <h1 className="text-center">List of Cars </h1>
                <table className="table table-striped">
                    <thead>
                        <tr>


                            <td>Car ID</td>
                            <td>Car Name</td>
                            <td>Car Model</td>
                            <td>Car Daily Cost</td>

                        </tr>
                    </thead>

                    <tbody>
                        {
                            this.state.cars.map(
                                car =>
                                <tr key= {car.carId}>
                                    <td>{car.carId}</td>
                                    <td>{car.carName}</td>
                                    <td>{car.carModel}</td>
                                    <td>{car.dailyPrice}</td>
                                </tr>
                            )
                        }
                    </tbody>
                </table>

            </div>
        )
    }
}

export default withRouter(ListCars);

在尝试访问 /listcars 和 /listcustomers 时会发生这种情况,这可能是什么问题?我看到了一些 stackoverflow 答案,它们建议在路由中使用 'exact' 属性,但我已经在使用它,它在除这两个之外的所有路由上都运行良好。

谢谢!

编辑:

问题出在 index.js 中,我的 App 在 switch case 之外,这使它包含在所有路由中,现在看起来像这样,它解决了问题:

  <Router>
          <Switch>
            <Route exact strict path="http://localhost:3000/customerloginpage" component={RedirectCustomerLoginComponent}/>
            <Route exact strict path="/adminloginpage" component={AdminLoginPage}/>
            <Route exact strict path="/adminhomepage" component={AdminHomePage}/>
            <Route exact strict path="/listcustomers" component={ListCustomers}/>
            <Route exact strict path="/getcars" exact component={GetCars}/>
            <App/>
            </Switch>
      
    </Router>

【问题讨论】:

  • path="http://localhost:3000/customerloginpage" 路径似乎不正确,应该只是 path="/customerloginpage"。如果这不是问题,我认为没有办法在Switch 组件中匹配和呈现多个路由。您能否尝试创建一个 running 代码框来重现问题,以便我们实时检查和调试?
  • 我编辑了我的帖子,我发现了问题。关于该路径,我在其上使用 window.location.href 在端口 3001 上的此应用程序和端口 3000 上的应用程序之间切换
  • 啊,是的.... App 组件 outside Switch 将始终呈现。
  • @Ruthless 为什么App 组件在Switch 之外?尝试使用 react-router 主页中的示例:v5.reactrouter.com/web/guides/quick-start

标签: reactjs react-router


【解决方案1】:

尝试清理(重构)代码,而不是干扰大量“精确”、“字符串”。

我看到每条路径的路由都不同(即:/adminloginpage、/adminhomepage),因此无需发送垃圾邮件“exact”、“strict”。到目前为止,我看到的唯一使用“精确”或“严格”的用例是这个。

<Route exact path="/" ... /> // dashboard component
<Route path="/profile" ... /> 
<Route path="/mail" ... /> 

因为“/profile”路径包含“/”路径,所以当你转到“/profile”时,Router会先匹配“/”而不是“/profile”,因为“/”在上面“ /个人资料”。

您可以将&lt;Route exact path="/" ... /&gt; 放在列表路由的末尾以删除“精确”。

<Route path="/profile" ... /> 
<Route path="/mail" ... /> 
<Route path="/" ... /> // dashboard component

【讨论】:

  • 我根据您的建议更改了路线,并让“/”位于底部以不必使用 exact ,但即使它们在 switch 案例中也有关系吗?无论如何,遗憾的是没有区别,它仍然呈现“/”路径
  • 从第一个到最后一个(顶部->底部)切换匹配,这意味着顺序问题
猜你喜欢
  • 2023-04-03
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 2018-12-25
  • 2020-03-11
  • 1970-01-01
相关资源
最近更新 更多