0

I am working in Reactjs and using Nextjs,I am trying to add class(active) on "first id"/array first record (fetching from db) but right now working with statically "where id=1" instead of First record,How can i do this ? in other words i want to add active on "first record(in array)" not "where id=1", I tried with following code

{this.state.trending.map((post, index) => {
      return (
                <>
                <div className={`carousel-item ${post.id == 1 ? 'active' : ''}`}>
            )
       })}
2
  • why don't you use index instead of post.id , like this : <div className={carousel-item ${index == 0 ? 'active' : ''}}> Commented Nov 18, 2022 at 11:39
  • Does this answer your Q ? Commented Nov 18, 2022 at 11:40

2 Answers 2

0

Use index instead of post.id. Compare it to 0 instead of 1 and you should be just fine.

this.state.trending.map((post, index) => (
  <div className={`carousel-item ${index === 0 ? 'active' : ''}`}>
)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

this.state.trending.map((post, index) => (
  <div className={"carousel-item"+((index == 0)? 'active':'')}>
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.