[英]Why is my React router not functioning correctly?
I have developed an application using react and laravel to show a list of hotels.我使用 react 和 laravel 开发了一个应用程序来显示酒店列表。 When the user clicks on a single hotel, I want details of that hotel to show up in a type of 'single' view.
当用户单击单个酒店时,我希望该酒店的详细信息显示在“单个”视图中。 However, although in the main list view I am linking to the single page using the correct routing pattern, and I have defined the pattern in the router, when I click on the link I get taken to a '404 not found' page.
但是,尽管在主列表视图中我使用正确的路由模式链接到单个页面,并且我已经在路由器中定义了模式,但当我单击链接时,我会被带到“404 未找到”页面。
The same goes for the edit link for editing a post.用于编辑帖子的编辑链接也是如此。
Any ideas as to how to solve this issue would be much appreciated!任何有关如何解决此问题的想法将不胜感激!
Thanks,谢谢,
Robert Young罗伯特杨
London, UK英国伦敦
//App.js (router) //App.js(路由器)
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter, Route, Switch } from 'react-router-dom'
import Main from './Main'
import Header from './Header'
import SingleHotel from './SingleHotel'
import CreateHotel from './CreateHotel'
import EditHotel from './EditHotel'
class App extends Component {
render() {
return (
<BrowserRouter>
<div>
<Header />
<Switch>
<Route exact path='/' component={Main} />
<Route exact path='/hotels/:id' component={SingleHotel} />
<Route exact path='/create' component={CreateHotel} />
<Route exact path='/hotels/edit/:id' component={EditHotel} />
</Switch>
</div>
</BrowserRouter>
)
}
}
ReactDOM.render(<App />, document.getElementById('app'));
// Main.js (listing page) // Main.js(列表页面)
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Link } from 'react-router-dom';
class Main extends Component {
constructor(){
super();
this.state = {
hotels: [],
}
}
componentDidMount(){
fetch('/api/hotels')
.then(response => {
return response.json();
})
.then(hotels => {
this.setState({ hotels });
})
}
renderHotels() {
return this.state.hotels.map(hotel => {
return (
<li className="row hotel-row" key={hotel.hotel_id} >
<span className="col-sm-10">
<a href={`hotels/${hotel.hotel_id}`}>{ hotel.hotel_name }</a>
</span>
<span className="col-sm-2">
<a href={`/hotels/edit/${hotel.hotel_id}`}>Edit</a>
</span>
</li>
);
})
}
render () {
return (
<div className="hotels container">
<ul>
{ this.renderHotels() }
</ul>
<a className="btn btn-success create-hotel" href="/create">Add a new hotel</a>
</div>
);
}
}
export default Main
If you are getting the 404
from your web server that means:如果您从 web 服务器获取
404
,这意味着:
<a/>
tag causes a page refresh and you server doesn't have any matching route: <a/>
标签中的调度 onClick 事件导致页面刷新并且您的服务器没有任何匹配的路由: I recommend you to use Link
instead of <a/>
, because it internally use history and also won't trigger a full page refresh while an <a />
tag naturally will:我建议您使用
Link
而不是<a/>
,因为它在内部使用历史记录并且也不会触发整个页面刷新,而<a />
标签自然会:
Replace this:替换这个:
<a href={`/hotels/edit/${hotel.hotel_id}`}>Edit</a>
To this:对此:
<Link to={`/hotels/edit/${hotel.hotel_id}`}>Edit</Link>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.