Skip to content

How to change an image src on hover? #5766

Answered by ghost
DiogoM14 asked this question in Q&A
Discussion options

You must be logged in to vote

Hello,

I was able to figure out a solution :)
Click here to view on CodeSandbox or view the code below

export default function DynamicImage() {
  const image1 = 'url("ts.png")';
  const image2 = 'url("jss.png")';

  const [image, setImage] = useState(image1);

  return (
    <Image
      style={{ content: image }}
      onMouseEnter={() => setImage(image2)}
      onMouseOut={() => setImage(image1)}
      width={200}
    />
  );

You can modify my example to include your desired transition styling.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@shakti177
Comment options

Answer selected by DiogoM14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants