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

Animation is run at every re-render #6

Open
Kyliathy opened this issue Apr 20, 2020 · 2 comments
Open

Animation is run at every re-render #6

Kyliathy opened this issue Apr 20, 2020 · 2 comments

Comments

@Kyliathy
Copy link

Hey :). I'm using functional components and useState. Simply changing the value of a textbox (via the function provided by setState) will cause the animation to be re-run. Maybe I'm missing something :).

@vishalkumarsinghvi
Copy link

Can you share your piece of code because if you are using hooks so we have to maintain some rules in hooks..

@roackb2
Copy link

roackb2 commented Sep 7, 2020

@Kyliathy @vishalkumarsinghvi

I faced similar situation and I found the cause of the problem.

The old code cause the animation to start again every time render is called:

    render() {
       let animate = { left: ['0px', '250px']}
        return (
            <Tweenful.div
                className="tween-box row ml-2 mb-2 mr-2 mt-2"
                duration={1500}
                easing={elastic(1, 0.25)}
                style={{ position: 'relative' }}
                animate={animate}>
                <video id="video-elem" controls width="320" height="240"></video>
            </Tweenful.div>
        )
    }

What is going on is that the animate object is created every time render is called,
so the animation passed to Tweenful.div is different object, this cause Teenful to start animation again.

One working solution is to put the animate object in component state, like following:

    constructor() {
        super();
        this.state = {
            animate: { left: ['0px', '300px'] }
        }
    }

And in your render function, use the animate object in state, it will be the same reference (object) across different render calls

            <Tweenful.div
                className="tween-box row ml-2 mb-2 mr-2 mt-2"
                duration={1500}
                easing={elastic(1, 0.25)}
                style={{ position: 'relative' }}
                animate={this.state.animate}>
                <video id="video-elem" controls width="320" height="240"></video>
            </Tweenful.div>

Then it just works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants