0

i am just trying to change the image but it does not work

I have checked the links they are working but not the onclick function

protfolio.js

import Image from 'next/image';
import React from 'react';

const Portfolio = () => {
  const changeImage = () => {
    const image = document.getElementById('myImage');
    if (image.src.match("https://images.unsplash.com/photo-1506905925346-21bda4d32df4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2340&q=80")) {
        image.src = "https://images.unsplash.com/photo-1594717527389-a590b56e8d0a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2070&q=80";
    }
}
  return (
    <div className='max-w-[1240px] mx-auto py-16 text-center'>
      <button onClick={changeImage} >Travel Photos</button>
      <div className='grid grid-rows-none md:grid-cols-5 p-4 gap-4'>
        <div className='w-full h-full col-span-2 md:col-span-3 row-span-2'>
          <Image
            src='https://images.unsplash.com/photo-1506905925346-21bda4d32df4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2340&q=80'
            id='myImage'
            alt='/'
            layout='responsive'
            width='677'
            height='451'
            onClick={changeImage}
          />
        </div>
      </div>
    </div>
  );
};

export default Portfolio;
4
  • 1
    you should start using useState and useEffect hooks for this kind of needs Commented Aug 15, 2022 at 11:58
  • 1
    If anything causes this component to re-render, the changes you manually made to the DOM are lost. Manually changing the DOM is almost always the wrong approach in React. Commented Aug 15, 2022 at 12:00
  • make sure things aren't refreshing your page, you may need to force it to not refresh. Also, make sure that nothing is covering your onClick preventing the click. Commented Aug 15, 2022 at 12:03
  • can you tell what to change Commented Aug 15, 2022 at 12:21

2 Answers 2

1

Is there any reason you are not using state for this?

const [imageUrl, setImageUrl] = useState('https://images.unsplash.com/photo-1506905925346-21bda4d32df4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2340&q=80')

And on image onClick

onClick={()=> changeImage(imageUrl); }

And your function would be like this

const changeImage = (currentImageUrl) => {
    
    if (currentImageUrl.match("https://images.unsplash.com/photo-1506905925346-21bda4d32df4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2340&q=80")) {
       setImageUrl("https://images.unsplash.com/photo-1594717527389-a590b56e8d0a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2070&q=80")
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

I would highly recommend you to use the useRef to replace the src for the image

const imageRef=useRef();

bind ref to image

<img ref={imageRef} />

Use imageRef to change the src like this

imageRef.current.src = {image}

Hope it will help to resolve your issue.

5 Comments

in this imageRef.current.src = {image} do i have to change image with the image link. can you please specify how to use this
yes, {image} is what we need to change. it can be static url or dynamic
can you please tell how to write the code
like that imageRef.current.src = 'image url'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.