I want to make this simple task with Ref. First, you fill in how many buttons you want to generate, and then when you click on even I want to add class light to all buttons with even value and the same with odd value when clicking on odd. But this effect is simple with Vanilla JS just add querySelectorAll and after click add or remove class light, but in React I have a problem with understanding these refs case. When is only one ref I want how to do this but when I want to generate a few of them?
import React, { useState, useRef } from "react";
import "./style.css";
export default function App() {
const [number, setNumber] = useState("");
const evenButtonsRef = useRef([]);
const oddButtonsRef = useRef([]);
let buttons = [];
const generateButtons = () => {
for (let i = 1; i <= number; i++) {
buttons.push(
<button
ref={i % 2 === 0 ? evenButtonsRef : oddButtonsRef}
className={`${i % 2 === 0 ? "button even" : "button odd"}`}
>
{i}
</button>
);
}
return buttons;
};
const lightEvenButtons = () => evenButtonsRef.current.classList.add("light");
const lightOddButtons = () => oddButtonsRef.current.classList.add("light");
return (
<div>
<p>How many buttons?</p>
<input
type="text"
onChange={e => setNumber(e.target.value)}
placeholder="Number from 1 to 20"
/>
{number && (
<>
<div className="button-group">
<button onClick={lightEvenButtons}>Even</button>
<button onClick={lightOddButtons}>Odd</button>
</div>
<div className="button-group">{generateButtons()}</div>
</>
)}
</div>
);
}
Link to code: https://stackblitz.com/edit/refbuttons?file=src/App.js