1

I have below code in React + styled jsx.

I want to change the color of an array when the number of elements in it is a multiple of 4. (For example, when the key is 4 and 8 and..etc)

Is there any smart way to achieve it?

<div>
  <div className="app">
    <div className="seq">
      {new Array(32).fill().map((v, i) => (
        <div className="box" key={i} onClick={() => handleClick(i)}></div>
      ))}
    </div>
  </div>
</div>;

<style jsx>{`
  .app {
    background-color: rgb(37, 37, 37);
    min-height: 100vh;
    width: vw;
    display: flex;
    flex-flow: column;
  }
  .box {
    width: 30px;
    height: 30px;
    background-color: rgb(25, 255, 217);
    border-color: rgb(37, 37, 37);
    border-width: 2px;
    border-style: solid;
    border-radius: 15%;
  }
`}</style>;

1 Answer 1

1

Just need to add a simple condition:

className={ i > 0 && i % 4 === 0 ? 'box-with-diff-color' : 'box'}
Sign up to request clarification or add additional context in comments.

2 Comments

This will return true when your i is 0 and 1 .
Thanks, you're right. It return true with 0. I'll edit the answer

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.