B
B
Bogdnan1232020-11-16 18:51:49
React
Bogdnan123, 2020-11-16 18:51:49

How to create an indicator with React?

I need to make such an indicator using React 5fb29fc77170e031471280.png
How can I render for example 6 divs, and depending on the number, some of them are filled, some are empty... For example, I got a number from the back 5 - 5 filled , 1 - empty

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-11-16
@Bogdnan123

const Indicator = ({ value, max = 6 }) => (
  <div className="indicator">
    {[...Array(max)].map((n, i) => (
      <div className={`indicator-cell ${i < value ? 'active' : ''}`}></div>
    ))}
  </div>
);

.indicator-cell {
  background: white;
}
.indicator-cell.active {
  background: red;
}

<Indicator value={4} />
<Indicator value={1} />
<Indicator value={8} max={12} />

https://jsfiddle.net/f89hvusy/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question