Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using hover-effect in a React component #15

Open
Nicknyr opened this issue Sep 2, 2019 · 20 comments
Open

Using hover-effect in a React component #15

Nicknyr opened this issue Sep 2, 2019 · 20 comments
Labels
wontfix This will not be worked on

Comments

@Nicknyr
Copy link

Nicknyr commented Sep 2, 2019

How would I use hover-effect in a React component? I've tried the following but I can't seem to get it to work. Had someone on Stack Overflow recommend using refs in place of document.querySelector.

import React, { Component } from 'react';
import hoverEffect from 'hover-effect';
import kakko from '../assets/kaapo.jpg';
import {kakkoRangers} from '../assets/kakko-rangers.jpg';
import {ladyLiberty} from '../assets/ladyliberty3.png';
import styled, { css } from 'styled-components';

const STYLES = styled.div`
  height: 100vh;
  width: 100%;

  .my-div {
    height: 20em;
    width: 30em;
  }
`;

class Test extends Component {
  constructor(props) {
    super(props);

    this.state = {

    }
    this.myRef = React.createRef();
  }

  render() {
    const animation = new hoverEffect({
          parent: this.myRef.current,
          intensity: 0.3,
          image1: '..assets/kaapo.jpg',
          image2: '..assets/kakko-rangers.jpg',
          displacementImage: '../assets/ladyliberty3.png'
      });

    return (
      <STYLES>
        <div>
          <h1>Test</h1>
          <div ref={this.myRef} className="my-div">
          </div>
        </div>
      </STYLES>
    );
  }
}

export default Test;

@robin-dela
Copy link
Owner

Hey Nick,

Sorry for the late reply, unfortunately I don't have React knowledge so I won't be able to help you.
However if you check the dependant projects you'll find some using react, it might help you!

Cheers

@robin-dela robin-dela added the wontfix This will not be worked on label Oct 10, 2019
@Marhime
Copy link

Marhime commented Oct 28, 2019

Hey @Nicknyr,

You should create your instance inside ComponentDidMount instead of the render() function.

Something like that:

componentDidMount() {
       const animation = new hoverEffect({
          parent: this.myRef.current,
          intensity: 0.3,
          image1: '..assets/kaapo.jpg',
          image2: '..assets/kakko-rangers.jpg',
          displacementImage: '../assets/ladyliberty3.png'
      });
  }

@MarcJerschov
Copy link

MarcJerschov commented Jun 9, 2020

Hey guys,
Did anybody managed to get it working with react? I'm getting massive errors

I'm getting the mistake
'./hover' does not contain a default export (imported as 'hoverEffect').

Regards:)

@develord
Copy link

develord commented Jun 10, 2020

its worked for me
import hoverEffect from 'hover-effect/dist/hover-effect.js'
and i changed e=require("gsap/TweenMax") to e=require("gsap") in first line
after that just use hoverEffect like you want

@develord
Copy link

tested after fix
work in vue and react

@Louhla
Copy link

Louhla commented Jun 23, 2020

Does somebody have a full example? I am using functional components, but still, don't understand why to define a const animation if I am not using it anywhere.

@larrycoal
Copy link

import React,{useRef,useEffect} from 'react'

let var=useRef(null)
useEffect(()=>{
new hovereffect({
parent:var,
image1: image path
image2:image path
displacementImage:path
})
},[]}

.........

var=el}>

this worked for me

@davidhavlin
Copy link

tested after fix
work in vue and react

How? Im working in Vue especially nuxt, and i cant figure out how i have to implement this.. import just doesnt work

@develord
Copy link

develord commented Sep 4, 2020

tested after fix
work in vue and react

How? Im working in Vue especially nuxt, and i cant figure out how i have to implement this.. import just doesnt work

Try to install the repo in my git i tested to in nuxt

@vishwajeetraj11
Copy link

Hey guys,
Did anybody managed to get it working with react? I'm getting massive errors

I'm getting the mistake
'./hover' does not contain a default export (imported as 'hoverEffect').

Regards:)

Yes It Worked

Component
import React, { useEffect, useRef } from "react";
import hoverEffect from 'hover-effect'
import "./Displacement.scss"
import Logo from "../assets/1.png"
import Logo2 from "../assets/2.png"
import Logo3 from "../assets/3.png"

const Displacement = () => {
const img = useRef(null)

useEffect(() => {
    const effect = new hoverEffect({
        parent: img.current,
        intensity: 0.3,
        image1: Logo,
        image2: Logo2,
        displacementImage: Logo3
    })   
})



return (
    <div>
    <div ref={img} className="img-container">
    </div>
    </div>
)

}

export default Displacement

SCSS
.img {
width: 100%;
height: 100%;
object-fit: cover;
}

.img-container {
width: 60rem;
height: 60rem;
}

@FranchescoLivado
Copy link

FranchescoLivado commented Jun 18, 2021

Would anyone know how to use the .next() function of the library inside an onClick event?

Hey guys,
Did anybody managed to get it working with react? I'm getting massive errors
I'm getting the mistake
'./hover' does not contain a default export (imported as 'hoverEffect').
Regards:)

Yes It Worked

Component
import React, { useEffect, useRef } from "react";
import hoverEffect from 'hover-effect'
import "./Displacement.scss"
import Logo from "../assets/1.png"
import Logo2 from "../assets/2.png"
import Logo3 from "../assets/3.png"

const Displacement = () => {
const img = useRef(null)

useEffect(() => {
    const effect = new hoverEffect({
        parent: img.current,
        intensity: 0.3,
        image1: Logo,
        image2: Logo2,
        displacementImage: Logo3
    })   
})



return (
    <div>
    <div ref={img} className="img-container">
    </div>
    </div>
)

}

export default Displacement

SCSS
.img {
width: 100%;
height: 100%;
object-fit: cover;
}

.img-container {
width: 60rem;
height: 60rem;
}

@amenkhnissi
Copy link

hi , You must call the function "animation" (invoke it) so it can work , you must use use Effect

@ObaidNadeem
Copy link

does anybody know how do I run it with prev & next buttons ?

@Ajlveloper
Copy link

Hello, can someone explain to me why importing hover Effect gives me an error? I am using React.

@mejed-alkoutaini
Copy link

Hi, in order to use the Next & prev buttons with React, I used this way:

let effect;
useEffect(() => {
  effect = new hoverEffect({
    parent: img.current,
    intensity: 0.3,
    image1: image,
    image2: image2,
    displacementImage: image3,
  });
});

return (
  <div>
    <div ref={img} className="img-container">
      <button onClick={() => effect.next()}>Next</button>
      <button onClick={() => effect.previous()}>Prev</button>
    </div>
  </div>
);

note this way will not loop through the images, so for example, if you have 2 images, clicking the next button for the 2nd time will not work until you click the prev button, you could improve it by using a state to check the image index and a new function to call Next or Prev depends on the index

@fet-lains
Copy link

tested after fix work in vue and react

Hey, bro. How do you make it work in vue? I managed to implement this plugin in my project, everything is fine, canvas is created, but height is 0, therefore there is no image :(
Screenshot 2023-04-28 at 09 40 25

@mejed-alkoutaini
Copy link

make sure to give the parent div a fixed height, this way the canvas gonna take the same height that the parent div has

@fet-lains
Copy link

make sure to give the parent div a fixed height, this way the canvas gonna take the same height that the parent div has

Yeah, I've already figured it out and implemented. It works! The only trouble was to make it adaptive, as I have three photos in a row and the screen is full width, so starting 1200px and higher the width and height change. But I solved it too. If setting height inline in html template it does not work. So I used a dynamic variable and height property in styles. In this case it works and it's adaptive! I can share the code.

@osamalmezouari
Copy link

Hi everyone! Does anyone know how to trigger a hover effect using the parent element ? I tried using the mouseenter and mouseout events but it didn’t work. Any ideas?
i am using react

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

17 participants