I have a component with images and want them to open bigger as a slide (different component) when clicked.
The component where the all the images are
import {data} from '../works/works'
import Work from '../components/Work'
function Portfolio() {
const WorkData = data.map((item) => {
return <Work
id={item.id}
title={item.title}
local={item.local}
src={item.src}
key={crypto.randomUUID()}
/>
})
return WorkData
}
export default function works() {
return (
<div className="works">
<Portfolio />
</div>
)
}
the component where images functionalities are
export default function Work(props) {
let src = require(`../imgs/works/${props.src}`)
function click(id) {
console.log(id)
}
return (
<div onClick={ () => click(props.id) } id={`${props.id}`} className="work" style={{backgroundImage:`url(${src})`}}>
<div className="title">
<h4>{props.title}<br/>{props.local}</h4>
</div>
</div>
)
}
and my slide component
import {useState} from 'react'
export default function Slide() {
const [state, setState] = useState('hidden')
return (
<div className="slide" style={{visibility: state}}>
<div className="image">
<img src={src} alt={alt} slide={slide}></img>
</div>
<div className="controls"></div>
</div>
)
}
i know that i can do this with use state but i cannot, and i can be mistaken, figure out how to do this
The idea is when user clicks on each image, the image will open bigger on the slide component.
2
Answers
You can define a new state, let’s say:
Then create a Modal component that takes in 4 props, put this modal component inside of slide component.
Anytime user clicks on the image, set the state to that image’s id. Anytime it is closed, set the state to null so that it is not displayed anymore.
Below I have shared a simple code to display multiple images, and clicking on them opens them in full screen, click again to close the full screen mode.
You can click on "Run code snippet" in the answer itself below to checkout the web app.