V
V
Verstatel30002019-09-19 15:54:55
JavaScript
Verstatel3000, 2019-09-19 15:54:55

How to pause the dismount of a component so that the animation will work?

I want to make animated page transitions using a hook, but how can I make the component hide animation work before the component is unmounted.
here is the HOK code:

import React from 'react';

const withFade = (PassedComponent) => {
    return class extends React.Component {
        state = {
            wrapperStyle: {
                transition: 'opacity .2s ease-out',
                opacity: '0'
            }
        }


        unfade = () => this.setState({ wrapperStyle: { ...this.state.wrapperStyle, opacity: '1' } })
        fade = () => this.setState({ wrapperStyle: { ...this.state.wrapperStyle, opacity: '0' } })
        componentDidMount = () => { this.unfade() }
        componentWillUnmount = () => { this.fade() }
        render() {
            return (
                <div style={this.state.wrapperStyle}>
                    <PassedComponent
                        {...this.props}
                    />
                </div>
            )
        }
    }
}
export default withFade

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
kn1ght_t, 2019-09-19
@kn1ght_t

https://github.com/reactjs/react-transition-group

A
Anton Neverov, 2019-09-19
@TTATPuOT

Perhaps the componentWillUnmount() method will suffice

P
profesor08, 2019-09-20
@profesor08

Never stop. You need to do it differently, you must have some condition by which the component is displayed. Let it be some show property. If it is true, then the component appears, if it is false, then it disappears. You will have to create one more property that will be responsible for rendering the component in the DOM, shouldRender. So, depending on the show, substitute the necessary styles for the component, better animation on @keyframes. Further, the animated element can be set onAnimationEnd, and in it, depending on the value of show, set the value for shouldRender, if shouldRender is false, then the component will disappear from the DOM.
Read more here:
https://dev.to/michalczaplinski/super-easy-react-m...

A
Alexander Batalov, 2019-09-20
@radist2s

If you expect more animations, then you can use special libraries for React. I use Framer Motion, by the way, your task is solved within the framework of the functionality of the same module.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question