Answer the question
In order to leave comments, you need to log in
Why is there an error in the function of passing the index of an image in a lightbox?
https://codesandbox.io/s/elastic-voice-7nynb?file=...
I'm trying to hang an onclick inside the slider for each picture to open a lightbox with a picture corresponding to the index slide. If I uncomment the line “onClick={openLightboxOnSlide(key)}” in my code, I get an error: Too many re-renders. React limits the number of renders to prevent an infinite
loop
Answer the question
In order to leave comments, you need to log in
Because you don't see the difference between a function and the result of calling it.
Add () =>
before openLightboxOnSlide(key)
(by the way, why the hell is the variable containing the index named key?).
But in general, an additional method is not needed. And you don't need to store the object in lightIndex. Let the index take a default value that cannot be a valid index (for example null
, or -1
) - this will mean that nothing needs to be shown:
const [ lightIndex, setLightIndex ] = useState(null);
<img onClick={() => setLightIndex(key)} />
<ReactBnbGallery
photos={photos}
show={lightIndex !== null}
activePhotoIndex={lightIndex}
onClose={() => setLightIndex(null)}
/>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question