Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何访问ReactJS中的下一篇博文_Reactjs_React Router - Fatal编程技术网

如何访问ReactJS中的下一篇博文

如何访问ReactJS中的下一篇博文,reactjs,react-router,Reactjs,React Router,我在构建React JS应用程序时遇到一些问题。所以,我有一个博客,里面有帖子,每个帖子都有详细的视图。从这个详细视图中,我需要两个按钮,一个将用户重定向到下一篇文章,另一个将用户重定向到上一篇文章。我尝试过几种方法来实现这一目标,但都没有奏效。还有一个细节需要说明的是,这些帖子不是一个接一个的,我的意思是其中一个有id 12,下一个有id 19 这是我代码的一部分: export default class DetailView extends React.Component { const

我在构建React JS应用程序时遇到一些问题。所以,我有一个博客,里面有帖子,每个帖子都有详细的视图。从这个详细视图中,我需要两个按钮,一个将用户重定向到下一篇文章,另一个将用户重定向到上一篇文章。我尝试过几种方法来实现这一目标,但都没有奏效。还有一个细节需要说明的是,这些帖子不是一个接一个的,我的意思是其中一个有id 12,下一个有id 19

这是我代码的一部分:

export default class DetailView extends React.Component {

constructor(props) {
    super(props);
    this.state = {
      item: [],
      isLoaded: false,
    }
}

componentDidMount() {

    const articleID = this.props.match.params.articleID;
    fetch(`http://localhost:8000/api/${articleID}`)
        .then(res => res.json())
        .then(json => {
        this.setState({
            isLoaded: true,
            item: json,
        })
        });
}

render() {

    return(
        <div className="detailView font">
            blah blah, here the important part comes
                        <Row className="padding">
                            <Col sm={12} md={12} lg={6} xl={6}>
                                <Button variant="outline-secondary"><Link to={''}>Previous project</Link></Button>
                            </Col>
                            <Col sm={12} md={12} lg={6} xl={6} className="text-right">
                                <Button variant="outline-secondary"><Link to={''}>Next project</Link></Button>
                            </Col>  
                        </Row>
                </Container>
            </div>
        </div>
    )
}
导出默认类DetailView扩展React.Component{
建造师(道具){
超级(道具);
此.state={
项目:[],
isLoaded:false,
}
}
componentDidMount(){
const articleID=this.props.match.params.articleID;
取回(`http://localhost:8000/api/${articleID}`)
.then(res=>res.json())
。然后(json=>{
这是我的国家({
isLoaded:是的,
项目:json,
})
});
}
render(){
返回(
废话废话,重要的部分来了
以前的项目
下一个项目
)
}

}

如果你控制了服务器,你可以添加一个端点,比如
/posts/:id/next
,然后服务器将决定下一篇文章(使用post-date、id,实际上是最适合博客设计的任何东西)。

如果你控制了服务器,您可以添加一个端点,如
/posts/:id/next
,然后服务器将决定下一篇文章,而不是需要知道下一篇文章id的组件(使用发布日期、id,实际上是最适合博客设计的).

请提供您尝试过的示例并添加上下文,例如,您的api响应了什么?我会,但我不知道如何实现。请提供您尝试过的示例并添加上下文,例如,您的api响应了什么?我会,但我不知道如何实现。