I've got the following code:
let ctx = ref.current.getContext("2d");
if(ctx){
ctx.lineWidth=1; // here is ok!
ctx.strokeStyle=props.barStroke??"darkgray";// here is ok!
ctx.fillStyle=props.barFill??"blue"; // here is ok!
props.data.map((v,i)=>{
ctx.fillRect(i*10,0,10,props.height); //here it complain about ctx is possibly null!!
ctx.strokeRect(1*10,0,10,props.height);
})
}
I've checked ctx and so ctx.lineWidth works without errors, but inside the callback, it signals that the object could be possibly null. If I use ctx?.method code works anyway, so how can be ctx, checked for null outside the callback, became null inside it?
Here a link showing the problem.