The following quick example in TypeScript shows a way to get typed refs without using inline (which are suppose to be bad for performance). It is however rather ugly having to define two variables (refAnimationWrapper
and refAnimationWrapperHandler
) to define one ref. Does anyone have a simpler solution, could @decorators maybe be a solution?
https://www.typescriptlang.org/docs/handbook/decorators.html
import * as React from 'react';
import {TweenMax} from 'gsap';
export class TransitionDummy extends React.Component<any, any> {
private transitionDuration = 0.4;
private refAnimationWrapper:HTMLDivElement;
private refAnimationWrapperHandler = (ref:HTMLDivElement) => this.refAnimationWrapper = ref;
constructor(props) {
super(props);
}
public componentWillEnter(done) {
TweenMax.fromTo(this.refAnimationWrapper, this.transitionDuration, {opacity: 0}, {opacity: 1, onComplete: done});
}
public componentWillLeave(done) {
TweenMax.to(this.refAnimationWrapper, this.transitionDuration, {opacity: 0, onComplete: done});
}
public render() {
return (
<div ref={this.refAnimationWrapperHandler}>
{this.props.children}
</div>
);
}
}