[英]React-router doesn't work. How can i fix it?
I'm trying to do a SPA with react-router.我正在尝试使用 react-router 进行 SPA。 I'm trying to make a two components PhotosList - this is a list of photos and Photo - this is a transition for watching single photo.
我正在尝试制作两个组件 PhotosList - 这是照片列表和照片 - 这是观看单张照片的过渡。 Component with path='/' work, but component with path='/photo' doesn't work and I have a 404 error.
带有 path='/' 的组件可以工作,但是带有 path='/photo' 的组件不起作用,并且出现 404 错误。 My site is https://mishindmitry.site
我的网站是https://mishindmitry.site
Here my code.这是我的代码。 AssyncApp.js
AssyncApp.js
class AsyncApp extends Component {
constructor(props) {
super(props);
this.state = {
isFetching: false,
error: false,
photos: []
};
this.handleScroll = this.handleScroll.bind(this);
}
componentDidMount() {
const { photos, getPhotos } = this.props;
window.addEventListener("scroll", this.handleScroll);
return getPhotos(photos);
}
componentWillUnmount() {
window.removeEventListener("scroll", this.handleScroll);
}
handleScroll() {
const { photos, getPhotos } = this.props;
const windowHeight = window.innerHeight;
const scrollTop = window.scrollY;
const clientHeight = document.body.clientHeight;
const scrollOnBottom = clientHeight - scrollTop;
if (windowHeight - scrollOnBottom === 0) {
return getPhotos(photos);
}
}
render() {
return (
<div>
<BrowserRouter>
<div>
<Switch>
<Route
path="/"
exact
render={() => {
return <PhotosList photos={this.props.photos} />;
}}
/>
<Route path="/photo" component={Photo} />
</Switch>
</div>
</BrowserRouter>
<div>
<button onClick={this.props.getPhotos} className="loadphoto_btn">
Загрузить еще
</button>
</div>
</div>
);
}
}
const mapStateToProps = state => {
return {
photos: state.photos
};
};
const mapDispatchToProps = dispatch => {
return {
getPhotos: photos => dispatch(getPhotos(photos))
};
};
export default connect(mapStateToProps, mapDispatchToProps)(AsyncApp);
Root.js根.js
import React, { Component } from "react";
import configureStore from "../configureStore/index.js";
import AsyncApp from "./AsyncApp.js";
import { Provider } from "react-redux";
const store = configureStore();
export default class Root extends Component {
render() {
return (
<Provider store={store}>
<AsyncApp />
</Provider>
);
}
}
PhotosList.js照片列表.js
export default class PhotosList extends Component {
render() {
return (
<ul className="flex-container">
{this.props.photos.map((photo, id) => (
<li key={id}>
<div className="user_info">
<Link to="/photo">
<img src={photo.urls.thumb} alt="photo" key={id} />
</Link>
<div className="info_container">
<div className="container__btn_like">
<button className="btn_like">
<svg
className="svg_img"
version="1.1"
viewBox="0 0 32 32"
width="16"
height="16"
aria-hidden="false"
>
<path d="M17.4 29c-.8.8-2 .8-2.8 0l-12.3-12.8c-3.1-3.1-3.1-8.2 0-11.4 3.1-3.1 8.2-3.1 11.3 0l2.4 2.8 2.3-2.8c3.1-3.1 8.2-3.1 11.3 0 3.1 3.1 3.1 8.2 0 11.4l-12.2 12.8z"></path>
</svg>
</button>
{" " + photo.likes}
</div>
<div className="user_photo_name">
<a href={photo.user.links.html} className="user_link">
<img
src={photo.user.profile_image.small}
className="user_img"
/>
<h3 className="user_name">{photo.user.name}</h3>
</a>
</div>
</div>
</div>
</li>
))}
</ul>
);
}
}
and Index.js和Index.js
render(<Root />, document.querySelector(".fixed-container"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.