I have a 2d matrix of divs in React, I want to implement a functionality in which the user holds mouse click and moves the mouse pointer over the divs to change the color of them. Is there a event that can help me do this in react.
import { useState } from "react";
import { useEffect } from "react";
function Grid (){
const [canvas, setcanvas] = useState([[]]);
const handledown = (e) => {
const d = e.target;
d.style.backgroundColor = "red";
}
const create = () => {
let a = []
for (var i = 0; i<30; i++){
a.push([]);
for (var j=0; j<30; j++){
var obj = {x: i, y: j, clr: "white"};
a[i].push(obj);
}
}
setcanvas(a);
}
useEffect(create, []);
const m = canvas.map((x)=> {
return (x.map((y) => {
const clr = x.color;
const cls = `cell ${x.x} ${x.x}`;
return (<div className = {cls} style={{backgroundColor: clr}} onClick ={handledown} > </div>);
}));
});
return <div className="canvas"> {m}</div>;
}
export default Grid;