Next.js Discord

Discord Forum

How to make getServerSideProps working in app directory ?

Answered
i_lost_to_loba_kreygasm posted this in #help-forum
Open in Discord
import Link from "next/link";
export default function Page({data}){
return <ul>
{data.map(post => (
  <li key={post.id}>
    <Link href={`/blog/${post.id}`}>
      <p>See Post {post.id}</p>
    </Link>
  </li>
))}
</ul>
}

export async function getServerSideProps(){
  let res =await  fetch('https://jsonplaceholder.typicode.com/users');
  let users =await res.json();
  return {props:{data}}
}
Answered by B33fb0n3
you can't. The functionallity changed and getServerSideProps is only for pages router. The good thing tho: you can use excatly the same directly inside your component:
import Link from "next/link";
export default function Page(){
let res =await  fetch('https://jsonplaceholder.typicode.com/users');
  let users =await res.json();

return <ul>
{users.map(post => (
  <li key={post.id}>
    <Link href={`/blog/${post.id}`}>
      <p>See Post {post.id}</p>
    </Link>
  </li>
))}
</ul>
}
View full answer

8 Replies

@i_lost_to_loba_kreygasm js import Link from "next/link"; export default function Page({data}){ return <ul> {data.map(post => ( <li key={post.id}> <Link href={`/blog/${post.id}`}> <p>See Post {post.id}</p> </Link> </li> ))} </ul> } export async function getServerSideProps(){ let res =await fetch('https://jsonplaceholder.typicode.com/users'); let users =await res.json(); return {props:{data}} }
you can't. The functionallity changed and getServerSideProps is only for pages router. The good thing tho: you can use excatly the same directly inside your component:
import Link from "next/link";
export default function Page(){
let res =await  fetch('https://jsonplaceholder.typicode.com/users');
  let users =await res.json();

return <ul>
{users.map(post => (
  <li key={post.id}>
    <Link href={`/blog/${post.id}`}>
      <p>See Post {post.id}</p>
    </Link>
  </li>
))}
</ul>
}
Answer
@i_lost_to_loba_kreygasm hi and thank you , but I have another issue
let's first resolve on issue. Is this issue already resolved? If yes, open a new thread with the new topic. In this case you can ping me so we can continue the conversation. If not, let's resolve it first
yes
ok
contuining a new thread
Happy to help