Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
Using nested routing in Next to get data from Firestore

I want to get data from Firestore in Next.JS like Reddit. For example (http://localhost:3000/r/BestBurger) in cloud firestore my data is in (/BestBurger/posts/posts/KrcvgXMX4HyNKPD1kRAR)

import { useRouter } from 'next/router';
import { useState, useEffect } from 'react';


import firebase from '../../firebase/clientApp';
import Card from '../../components/card/Card';
;




export default function Page() {
    const router = useRouter();
    const { page } = router.query;

    function usePosts() {
        const [posts, setPosts] = useState([])



        useEffect(() => {
            firebase
                .firestore()
                .collection(`${page}`).doc('posts').collection('posts')
                .onSnapshot((snapshot) => {
                    const newPosts = snapshot.docs.map((doc) => (
                        {
                            id: doc.id,
                            ...doc.data()
                        }))
                    setPosts(newPosts)
                }
                )
        }, [])

        return posts;
    }

    const posts = usePosts()
    console.log(posts)

    return (
        <>
            {
                posts.map((post) => (
                    <Card key={post.id}>
                        <h2>{post.title}</h2>
                        {post.body}
                    </Card>
                ))
            }
        </>
    )
}

Again, I want to get my page route to the .collection()

enter image description here

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!