Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
How to add Google Ads in a Feed after every 'n' number of Post using next.js

I want to create a feed where a Google Ad is shown after every 10 posts just like Instagram. I am using Firebase as my database and tailwind-CSS for the styling. How would I use Google Ads to implement this feature?

Here is my code for displaying a Feed

Feed.js

import {React, useState, useEffect} from "react";
import Navbar from "./Navbar";
import Post from "./Post";
import { onSnapshot, collection, query, orderBy } from "@firebase/firestore";
import { db } from "../firebase";

function Feed() {
  const [posts, setPosts] = useState([]);
  useEffect(
    () =>
      onSnapshot(
        query(collection(db, "posts"), orderBy("timestamp", "desc")),
        (snapshot) => {
          setPosts(snapshot.docs);
        }
      ),
    [db]
  );
  return (
    <div>
      <Navbar />
      <div className="pb-72">
        {posts.map((post) => (
          <Post key={post.id} id={post.id} post={post.data()} />
        ))}
      </div>
    </div>
  );
}

export default Feed;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The javascript map function has a second parameter - index - that tells you the index of the item in the array it is iterating. So you would want to make two key changes:

return (
  <div>
    <Navbar />
    <div className="pb-72">
      {posts.map((post, idx) => {
         // If true, you're on the tenth post
         const isTenthPost = (idx + 1) % 10 === 0

         // Note the addition of the React fragment brackets - your map call
         // has to return a single React component, so we add this to handle
         // the case where we want to return both the post and the Google ad.
         return (
           <>
             <Post key={post.id} id={post.id} post={post.data()} />
             { isTenthPost && <GoogleAdComponent /> }
           </>
         )
      })}
    </div>
  </div>
);

I'm not suggesting you copy and paste this exactly, but it should help you understand how to determine if you're on the nth post and how to conditionally display another component.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda