Fetch WordPress Blog Posts – Next.js (Server Side Props)

  1. Home
  2. Blog
  3. Fetch WordPress Blog Posts – Next.js (Server Side Props)

Fetch WordPress Blog Posts – Next.js (Server Side Props)

import React from “react”;
export async function getServerSideProps() {
  const res = await fetch(“https://wpsite.com/wp-json/wp/v2/posts”);
  const data = await res.json();
  return {
    props: {
      blogs: data,
    },
  };
}
const Blog = ({ blogs }) => {
  return (
    <div>
      {blogs.length === 0 ? (
        <div>Loading</div>
      ) : (
        blogs.map((blog, index) => (
          <div key=”{index}”>{blog.title.rendered}</div>
        ))
      )}
    </div>
  );
};
export default Blog;
Previous Post
React Native Installation using expo [Ubuntu 20.04 LTS]
Menu