How to make image elment block element in ReactJS

am building react image gallery, where I’ve display all the images on the browser. I have previous and next buttons display the previous and next images if a user clicks on any of them, which is working very well.
My problem is that I want this previous or next image to be displayed below the already displayed images, not next to them.
I’ve set the CSS display property of the image element to block but it is still displayed next to the other images
below is the code
import ReactImageMagnify from ‘@blacklab/react-image-magnify’;
import galleryData from ‘…/GalleryData’;
import ‘…/App.css’;
import { useState } from ‘react’;

function Image(){
const [images, setImages] = useState(galleryData);
const [currentIndex, setCurrentIndex] = useState(0);

const goToPreviousImage = ()=>{
if (currentIndex > 0){
setCurrentIndex(currentIndex - 1)

}

};

const goToNextImage = () =>{
if(currentIndex < images.length - 1){
setCurrentIndex(currentIndex + 1)

}

}

const currentImage = images[currentIndex];

return(
  
    <div className="App flex-container" >
      <button onClick={goToPreviousImage}>Previous</button>
    {
      images.map((image, index) => (
      <div key={image.id} className={`image-gallery-item ${index === currentIndex ? 'active' : '' }`}>
          <ReactImageMagnify 
          activationInteractionHint='hover'
          imageProps={{
            src:image.url,
            width: 200,
            height: 200
          }}
          magnifiedImageProps={{
            src: image.url,
            width: 800,
            height: 800
          }}
          portalProps={{
            id: 'portal-test-id',
            horizontalOffset: 20
          }}
          />
      </div>
  ))}
  <button onClick={goToNextImage}>Next</button>
  <img src={currentImage.url} className='next-img'/>
  
  
  </div>
  

);

};
export default Image;

CSS Code

.next-img{
display: block;
width: 300px;
height: 300px;
}

If we’re to believe that line emulates native HTML, className is not HTML. Perhaps React knows this and parses it out for attribute substitution. One wouldn’t see the need for any intervention by React and would just go with the proper name of the HTML attribute.

<img class="next-img" src={currentImage.url}>

We don’t see what you have defined in App.css apart from the .next-img class. But the class name ‘flex-container’ suggests that you have set the parent container to ‘display: flex’ which makes the children display next to each other unless you also set t to ‘flex-direction: column’.

It is JSX which is what is returned by a React functional or class component. ‘class’ is a reserved keyword in react and you have to use className instead.

@text7176740845 you code is very hard to read due to the lack of formatting. Either wrap code blocks in three backticks each or mark it and press </>

3 Likes

Ah, yes, JSX. My mistake.

1 Like