【发布时间】:2020-03-02 21:52:09
【问题描述】:
我对动态路由有点困惑,因为我是 NextJS 的新手。我希望它是这样的,如果有人点击我的页面标题,它会将他们带到一个包含相同标题和正文的新页面。为了实现这一目标,我可以做出哪些改变?我检查了许多资源,但它们要么是从 a 到 b,而且数据是硬编码的。
import React from 'react';
import axios from 'axios'
import Link from 'next/link';
class Abc extends React.Component{
state = {
title: '',
body: '',
posts: []
};
componentDidMount=()=>{
this.getBlogPost();
};
displayBody=(posts: Array<any>)=>{
if(!posts.length)
return null;
return posts.map((post,index)=>(
<div key={index}>
<Link href={`/post?title=${this.state.title}`} ><a>
{post.title}</a></Link>
<h2>{post.title}</h2>
<p>{post.body}</p>
</div>
));
};
render() {
console.log('state', this.state);
return (
<div>
<h2>Welcome to my app</h2>
<div className="blog">
{this.displayBody(this.state.posts)}
</div>
</div>
);
}
}
export default Abc
【问题讨论】:
标签: reactjs typescript next.js