0

I am trying to deal with a problem where I am using react-slick. I mapped a JSON file for images and it gives me the error "Object is possibly 'null'.ts (2531)". I have to keep strict typing so I just can't get rid of it. Here is the part of the code that causing the problem:

const [slider, setSliderRef] = useState(null);
... // Some other coding

const settings = {
... // Some settings
   afterChange: (i: React.SetStateAction<number>) => setSliderIndex(i),
}

... // Some more coding

{images.map((image: string | undefined, index: any) => (
   <Box>
     <img src={image} alt={index} className={className} onClick={() => slider.slickGoTo(index)} />
   </Box>
))}

for the word slider here it says: "Object is possibly 'null'.ts (2531)" and of course it makes trouble on Jenkins side.

I tried some ways but no luck. Any idea?

Best

osmancalisir
  • 147
  • 2
  • 10
  • 1
    It is caused by `useState(null)` and default value of `slider is null`. Try to pass an object to the slider. Eg. `useState({slickGoTo : (index: any) => ''});` – Łukasz Olszewski Jun 02 '21 at 09:30
  • although it does not solve that, it really helped a lot thank you so much @ŁukaszOlszewski :) I solved it after adding after adding "as any" to the reference and it workes fine now – osmancalisir Jun 02 '21 at 13:56

0 Answers0