Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
paints all elements of the list red
import { useRef, useEffect } from "react";
const Item = ({ element, pushNode }) => {
const node = useRef(null);
useEffect(() => {
pushNode(node);
}, [node]);
return <li ref={node}>{element}</li>;
};
const List = ({ elements, pushNode }) => {
return (
<ul>
{elements.map((element) => (
<Item element={element} pushNode={pushNode} />
))}
</ul>
);
};
const App = () => {
const elements = ["one", "two", "three"];
const nodes = [];
const pushNode = (node) => nodes.push(node);
const paintRed = (event) => {
nodes.forEach((node) => {
node.current.style.color = "red";
});
};
return (
<>
<List elements={elements} pushNode={pushNode} />
<button onClick={paintRed}>Paint it red</button>
</>
);
};
export default App;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question