0
  <div className="pagination">
  <br />
    <center>
        {[...Array(this.props.pages+1)].map((x, i) =>
          <h2 key={i} onClick={()=>this.demoMethod(i+1)} className="tc">{ i+1 }</h2>
        )}
    </center>
  </div>

Here in my code i wants to write if else statement . Actually it is a pagination logic. i wants to change color of h2 for active page. so i wants to write if statement inside this.

Please share any idea how i can do it..

1 Answer 1

1

You can write if else as follows:

// active is a boolean variable having value true or false
<div className="pagination" style={{color: active? 'red' : 'blue'}}>

Inside loop it can as follows:

{[...Array(this.props.pages+1)].map((x, i) => {
   if(x) {
      return <h2 key={i} onClick={()=>this.demoMethod(i+1)} className="tc">{ i+1 }</h2>
     } else {
       return <p>my paragraph</p>
      }
   }
)}
Sign up to request clarification or add additional context in comments.

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.