Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React js, limiting post shown

Hi i am just making a blog using next js and graph cms,i just know how to limit the number posts shown, for example if i have 50 posts in the backend i just need 1 post to show, like how to limit it?

here, this is what i am talking about

const posts = [
    {title: 'Sample Post 1', description: 'Lorem ipsum dolor sit amet'},
    {title: 'Sample Post 2', description: 'Lorem ipsum dolor sit amet'}
];

<div>
       {
        posts.map((posts, index) => (
          <div>
            <h1>{posts.title}</h1>
             <p>{posts.description}</p>
             
           </div>
        ))
       }
    </div>

if i run this all the post will be show, is there anyway to limit in JS?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can use slice

{
   posts.slice(0,1).map((posts, index) => (
     <div>
        <h1>{posts.title}</h1>
        <p>{posts.description}</p>       
     </div>
     ))
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading