I want to use the useRef
hook to change the style of a DOM element:
const Box = props => {
const box = useRef(0);
const onClick = () => {
box.current.backgroundColor = "blue";
};
return (
<div>
<div
ref={box}
style={{ width: "300px", height: "300px", backgroundColor: "red" }}
/>
<button onClick={onClick}>Change color of box</button>
</div>
);
};
However, if I click on the button the backgroundColor
of the box
doesn't change.
I also created a simple code snippet, which illustrates my problem.