dynamiczne użytkowanie w React
const data = ["James", "John", "Jessica", "Jamie"];
function App() {
const [visibilities, setVisibilities] = React.useState(() => data.map((x) => true));
const handleClick = (event) => {
const index = parseInt(event.currentTarget.dataset.index, 10);
const newVisibilities = [...visibilities];
newVisibilities[index] = !newVisibilities[index];
setVisibilities(newVisibilities);
};
return (
<div className="App">
{data.map((value, index) => (
<h1 data-index={index} onClick={handleClick} className={visibilities[index] ? "selected" : undefined}>
Hello {value}, you are {visibilities[index] ? "visible" : "hidden"}!
</h1>
))}
</div>
);
}
ReactDOM.render(<App />, document.querySelector("main"));
DevAndR