question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: componentWillPrepareUnmount (continueCallback)

See original GitHub issue

EDIT: After discussion in this thread I’ve amended componentWillRemount to the proposal.

I’ve been working a lot with animations in combination with react lately and while most lifecycle methods make transitions on components a walk in the park, I’ve struggled to get the scenario of unmounting a pleasant experience. In the end though I’ve come up with a lifecycle method proposal that could be incredibly powerful: componentWillPrepareUnmount (continueCallback) and componentWillRemount.

What they would do, when declared on a component, is to delay the actual unmount just as if nothing happened, until the component itself calls the provided callback, which then would resume the actual unmounting. If a component in a pending unmount state is restored before the actual unmount takes place, the unmount is canceled and the component treats the change as a regular update (componentWillUpdate fires, etc), with the addition of componentWillRemount being called to give you the chance to react specifically to this event.

Edge case mitigation: Ancestors override descendants. If an ancestor of a component using “unmount preparation” unmounts the descendant unmounts instantly without preparation, unless the ancestor also utilizes componentWillPrepareUnmount, which would give it some arbitrary time to prepare.

This would allow us to gracefully perform an animation on our components right before they’re really unmounted, letting component specific code decide how it should disappear.

For example:

var styles = {
    // .. some styling
};

var tweenOptions = {
    // .. some tween options
};

/**
 * A menu item that fades in and scales up on mount 
 * and then fades out and scales down on unmount.
 */
class MenuItem extends React.Component {
    componentWillMount () {
        this.tweens = {
            opacity: new Tween(0, tweenOptions),
            scaleY: new Tween(0, tweenOptions)
        };
        this.animateShow();
    }

    componentWillPrepareUnmount (continueCallback) {
        Promise.all([
            this.tweens.opacity.to(0),
            this.tweens.scaleY.to(0)
        ]).then(continueCallback);
    }

    componentWillRemount () {
        this.animateShow();
    }

    animateShow () {
        this.tweens.opacity.to(1);
        this.tweens.scaleY.to(1);
    }

    render () {
        return (
            <surface style={{...styles, this.tweens}}>
                {this.props.label}
            </surface>
        );
    }
}

And the usage:

class Menu extends React.Component {
    render () {
        var itemElements = this.props.items.map((item) => {
            return <MenuItem key={item.id} label={item.label}/>;
        });
        return (
            <surface style={{flexDirection: "row"}}>
                {itemElements}
            </surface>
        );
    }
}

Menu.propTypes = {
    items: React.PropTypes.arrayOf(
        React.PropTypes.shape({
            id: React.PropTypes.any,
            label: React.PropTypes.string
        })
    ).isRequired
};

What do you think?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Feb 19, 2016

@chenglou thought about this a lot (which resulted in react-motion). I found his thoughts on animation quite insightful.

0reactions
jimfbcommented, Feb 24, 2016

After some discussions with Sebastian, and thinking on this issue, I agree that this doesn’t elegantly handle the edge cases for the reasons mentioned above (classic problem with proposed solutions to animation). For this reason, I’m going to close out the issue. But feel free to continue the discussion on this thread, and we can re-open if our thinking changes dramatically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deferred unmounting RFC · Issue #128 · reactjs/rfcs - GitHub
In all of the following examples, the component would remain mounted for 5 seconds after the parent attempts to unmount it.
Read more >
reactjs - How to cancel a fetch on componentWillUnmount
As per React, you can call cancel() on the wrapped promise in componentWillUnmount to avoid setting state on an unmounted component.
Read more >
Hooks FAQ - React
Do Hooks replace render props and higher-order components? ... There are no plans to remove classes from React — we all need to...
Read more >
Set State in Callbacks in React - Jake Trent
State should only be set on mounted components, or those components that are inserted into the dom. If you attempt to set state...
Read more >
Method call callback setting state on unmounted component
Manually tracking whether the component is currently mounted and checking that before setting the state is a workaround but an antipattern ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found