Advertisement
Guest User

[id].js page code

a guest
Jan 21st, 2022
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import React from 'react';
  2. import axios from 'axios';
  3. import MarkdownIt from 'markdown-it/lib';
  4.  
  5. const Postpage = ({post}) => {
  6.  
  7. const md = new MarkdownIt();
  8. const htmlContent = md.render(post.attributes.content);
  9.  
  10. return (
  11. <>
  12. <article>
  13.  
  14. <header>
  15. <h1>{post.attributes.title}</h1>
  16. </header>
  17.  
  18. <p dangerouslySetInnerHTML={{__html: htmlContent}}></p>
  19. </article>
  20. </>
  21. )
  22. }
  23.  
  24. export default Postpage;
  25.  
  26.  
  27. export async function getStaticProps({params}) {
  28. const postsResult = await axios.get(`http://localhost:1337/api/posts/${params.id}?populate=image`);
  29. // const postsResult = await axios.get(`http://localhost:1337/api/posts/1?populate=image`);
  30.  
  31. return {
  32. props: {
  33. posts: postsResult.data
  34.  
  35. }
  36. }
  37.  
  38. }
  39.  
  40.  
  41. export async function getStaticPaths() {
  42. const postsRes = await axios.get("http://localhost:1337/api/posts?populate=image");
  43.  
  44. // console.log(postsRes);
  45. // console.log(postsRes.data);
  46.  
  47. const paths = postsRes.map((post) => {
  48. return { params: {id: post.id.toString()} }
  49. });
  50.  
  51. // const paths = { params: {id: '1' } }
  52.  
  53. return {
  54. paths,
  55. fallback: false
  56.  
  57. }
  58.  
  59.  
  60.  
  61. }
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement