我正在尝试构建一个显示博客文章列表的组件。当用户单击帖子时,它将呈现显示帖子详细信息的组件。但是,当用户在浏览器中单击“后退”按钮时,带有帖子列表的前一个组件将重新呈现,并失去前一个状态和滚动位置。有没有一种方法可以保存以前的状态和滚动位置,以便当用户单击后退按钮时,他们位于同一位置,并且帖子列表不会重新呈现,也不会松动滚动位置?
这是我的博客列表组件代码:
import axios from "axios";
import { Link } from "react-router-dom";
class About extends React.Component {
state = { posts: [] };
componentDidMount() {
axios.get("https://jsonplaceholder.typicode.com/posts").then(res => {
this.setState({ posts: res.data.slice(0, 10) });
});
}
render() {
const { posts } = this.state;
const postsList = posts.length ? (
posts.map(post => {
return (
<div className="card" key={post.id}>
<div className="card-body">
<Link to={"/view/" + post.id}>
<h5 className="card-title">{post.title}</h5>
</Link>
<p className="card-text">{post.body}</p>
</div>
</div>
);
})
) : (
<div className="text-danger text-center">No Posts yet...</div>
);
return <div>{postsList}</div>;
}
}
export default About;
这是我的博客详细信息组件:
import React from "react";
import { withRouter } from "react-router-dom";
import axios from "axios";
class PostDetail extends React.Component {
state = { post: null };
componentDidMount() {
let id = this.props.match.params.post_id;
axios.get("https://jsonplaceholder.typicode.com/posts/" + id).then(res => {
this.setState({ post: res.data });
});
}
render() {
const post = this.state.post ? (
<div className="card border-primary">
<div className="card-header">{this.state.post.title}</div>
<div className="card-body text-primary">
<p className="card-text">{this.state.post.body}</p>
</div>
</div>
) : (
<div className="text-center text-danger">Loading Post...</div>
);
return <div>{post}</div>;
}
}
export default withRouter(PostDetail);
答案 0 :(得分:1)
在点击帖子后,您必须使用state
将滚动位置存储在window.pageYOffset
this.setState({
scrollPosition: window.pageYOffset
});
然后,当您单击后退按钮时,必须使用componentDidMount
的方法设置窗口位置。
window.scrollTo(0, this.state.scrollPosition);
默认情况下,您可以将scrollPosition
的值设置为0
。
已更新
在这里,我已使用sessionStorage
来维持滚动位置,以进行演示。您还可以使用上下文API或redux存储进行管理。
以下是适合您的演示。 https://stackblitz.com/edit/react-fystht
import React from "react";
import axios from "axios";
import { Link } from "react-router-dom";
class Posts extends React.Component {
state = { posts: [] };
componentDidMount() {
axios.get("https://jsonplaceholder.typicode.com/posts").then(res => {
this.setState({ posts: res.data.slice(0, 20) }, () => {
this.handleScrollPosition();
});
});
}
// handle scroll position after content load
handleScrollPosition = () => {
const scrollPosition = sessionStorage.getItem("scrollPosition");
if (scrollPosition) {
window.scrollTo(0, parseInt(scrollPosition));
sessionStorage.removeItem("scrollPosition");
}
};
// store position in sessionStorage
handleClick = e => {
sessionStorage.setItem("scrollPosition", window.pageYOffset);
};
render() {
const { posts } = this.state;
const postsList = posts.length ? (
posts.map(post => {
return (
<div className="card" key={post.id}>
<div className="card-body">
<Link to={"/view/" + post.id} onClick={this.handleClick}>
<h5 className="card-title">{post.title}</h5>
</Link>
<p className="card-text">{post.body}</p>
</div>
</div>
);
})
) : (
<div className="text-danger text-center">No Posts yet...</div>
);
return <div>{postsList}</div>;
}
}
export default Posts;
希望这对您有帮助!
答案 1 :(得分:0)
只需检查以前的URL并调用API:
import React, {Component} from 'react';
import axios from "axios";
import { Link } from "react-router-dom";
class About extends React.Component {
constructor() {
super();
this.state = {
posts: [],
};
}
componentDidMount() {
var url = document.referrer
if (url && !url.includes("/view/")) {
axios.get("https://jsonplaceholder.typicode.com/posts").then(res => {
this.setState({
posts: res.data.slice(0, 10),
isAboutPageOpen: true
});
});
}
}
render() {
const { posts } = this.state;
const postsList = posts.length ? (
posts.map(post => {
return (
<div className="card" key={post.id}>
<div className="card-body">
<Link to={"/view/" + post.id}>
<h5 className="card-title">{post.title}</h5>
</Link>
<p className="card-text">{post.body}</p>
</div>
</div>
);
})
) : (
<div className="text-danger text-center">No Posts yet...</div>
);
return <div>{postsList}</div>;
}
}
export default About;