T
T
TheSnegok2022-04-11 13:35:44
React
TheSnegok, 2022-04-11 13:35:44

How to add logic for an attribute in React?

Good day!
There is a map loop that iterates over the array and creates inputs from it, and it is necessary that when creating only the first input have the checked attribute:

{dots.map((item, index) => <input checked type='radio' name="dot" key={index} className={s.Dot} />)}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wonderingpeanut, 2022-04-11
@TheSnegok

You have an index, you need to make the input with index 0 checked={true}. With a simple movement of the hand, we check whether the current index is equal to 0, and assign the resulting value to the checked property of the input.

...
<input checked={index === 0} {...props} />
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question