react-native-animatable icon indicating copy to clipboard operation
react-native-animatable copied to clipboard

Unmount animation

Open stevenyap opened this issue 8 years ago • 16 comments
trafficstars

I am not sure if this is a feature request but how do I add animation to the component if it is unmounting? I think it would be a pretty common case where we need to animate an element to slide in and then animate it to slide out when it is unmounting... how do we do this?

stevenyap avatar Jun 30 '17 09:06 stevenyap

I need this, can I achieve this with this library?

IvanCoronado avatar Feb 01 '18 17:02 IvanCoronado

Updates?

obetomuniz avatar Feb 09 '18 14:02 obetomuniz

pretty common case...

ringcrl avatar Mar 14 '18 01:03 ringcrl

Same here. I can hide the component but it feels against React philosophy :(

Almaju avatar Mar 22 '18 00:03 Almaju

any updates on this????

funkeeflow avatar Jun 27 '18 12:06 funkeeflow

I did a tricky to achieve this. Define visible in state and props respectively.

Check the change of this.props.visible.

componentWillReceiveProps(newProps) {
    if (this.props.visible !== newProps.visible) {
      if (newProps.visible) {
        this.setState({ visible: true }, () => this.view.slideInUp());
      } else {
        this.view.slideOutDown().then(() => this.setState({ visible: false }));
      }
    }
}

And use this.state.visible to detect whether render you view or not.

render() {
    return this.state.visible && (
        <Animatable.View
            ref={c=>{this.view=c}
        >
        {/* Any component here*/}
        </Animatable.View>
    );
}

May be a ugly way but work.

jordenchang55 avatar Aug 01 '18 08:08 jordenchang55

@jordenchang55 After the animation there will be an empty space that will flash out quickly when unmounting. So even though animation works, there will be sudden jerk when the empty space unmounts :(

hariks789 avatar Aug 11 '18 09:08 hariks789

Yes, I also encounter this issue. Add this to your style should resolve it.

{position: "absolute"}

jordenchang55 avatar Aug 15 '18 03:08 jordenchang55

+1 for the feature :)

ducpt2 avatar Aug 23 '18 06:08 ducpt2

I needed this for my pet project and I've pushed @jordenchang55 idea a bit further. It cannot really be done inside of componentWillUnmount but you can make a wrapper, which handles mount/unmount state. Proof of concept is here: https://github.com/hydropuzzle/react-native-animatable-unmountable

sobstel avatar Nov 19 '18 23:11 sobstel

+1 for feature

holgersindbaek avatar Apr 22 '19 16:04 holgersindbaek

Hello everyone, I have found something working arround!

Have a look at my code:

<FlatList data={listing} renderItem={({item,index})=> <Animatable.View animation={'fadeIn'} ref={p=>this[(index+1).toString()] = p} > <TouchableOpacity style={{backgroundColor:'#ddd',justifyContent:'center',alignItems:'center',paddingVertical:10,marginVertical:10}} onPress={()=>this._dismissActions((index+1).toString())} > <Text>{String.fromCharCode(index+65)}</Text> </TouchableOpacity> </Animatable.View> } keyExtractor={(item,index)=>index.toString()} />

and handle it's action :

_dismissActions=(ele)=>{ this[ele].fadeOutRight().then(()=>{ this[ele].setNativeProps({display:'none'}) }) }

Hope it will help

Ravi448 avatar May 22 '19 06:05 Ravi448

Its pretty trivial with hooks:


const MyComponent = (props) => {
  const ref = React.useRef();

  React.useEffect(() => {
    ref?.current?.fadeIn();

    return () => ref?.current?.fadeOut(); // as you know, this is the same as unmount ;)
  }, [props.id]);  // track some prop that changes 
 
  return (
    <Animatable.View ref={ref}>
      <View>
        ...stuff
      </View>
    </Animatable.View>
  );
};

jforaker avatar Feb 26 '21 20:02 jforaker

Its pretty trivial with hooks:

const MyComponent = (props) => {
  const ref = React.useRef();

  React.useEffect(() => {
    ref?.current?.fadeIn();

    return () => ref?.current?.fadeOut(); // as you know, this is the same as unmount ;)
  }, [props.id]);  // track some prop that changes 
 
  return (
    <Animatable.View ref={ref}>
      <View>
        ...stuff
      </View>
    </Animatable.View>
  );
};

does not work for me

slolkunchik avatar Mar 15 '21 19:03 slolkunchik

@slolkunchik - its a proof of concept. The inherent challenge with animating on unmount is the View gets destroyed - so if there is no view, how is it going to animate? You have to build in your own logic to do a fadeOut and then unmount the view. Thats the only way the View will still be alive and able to animate. See this example for a more complete implementation.

jforaker avatar Mar 15 '21 19:03 jforaker

@slolkunchik - its a proof of concept. The inherent challenge with animating on unmount is the View gets destroyed - so if there is no view, how is it going to animate? You have to build in your own logic to do a fadeOut and then unmount the view. Thats the only way the View will still be alive and able to animate. See this example for a more complete implementation.

yes, I've got it. It works now. Thanks. I just needed to adapt its behavior to onClick.

slolkunchik avatar Mar 16 '21 09:03 slolkunchik