I'm trying to transition between 3 conditionally rendered components when I click on them. I'm trying to achieve this by using TransitionGroup
from the react-transition-group
. This is what I have so far:
<div className="transitionContainer">
<Transition in={this.state.validFiles && this.state.dotted} timeout={duration}>
{state => (
<>
{this.state.validFiles && this.state.dotted ? (
<div className={`col fade fade-${state}`} onClick={this._handleDotted}>
<DottedPortrait state={this.state} />
</div>
) : null}
</>
)}
</Transition>
<Transition in={this.state.validFiles && this.state.square} timeout={duration}>
{state => (
<>
{this.state.validFiles && this.state.square ? (
<div className={`col fade fade-${state}`} onClick={this._handleSquare}>
<SquaredPortrait state={this.state} />
</div>
) : null}
</>
)}
</Transition>
<Transition in={this.state.validFiles && this.state.line} timeout={duration}>
{state => (
<>
{this.state.validFiles && this.state.line ? (
<div className={`col fade fade-${state}`} onClick={this._handleLine}>
<LinePortrait state={this.state} />
</div>
) : null}
</>
)}
</Transition>
</div>
While this code works for the entering state of each conditional render, the exit animation is not because the component is unmounted before transition group can apply the exiting
and exited
classes. What's the best way to tackle this?
Below is the handle
functions for these components:
handleValidFiles = () => {
console.log(this.state);
this.setState({
validFiles: true,
dotted: true,
square: false,
line: false
});
};
_handleDotted = () => {
console.log(this.state);
this.setState({
dotted: !this.state.dotted,
square: !this.state.square
});
};
_handleSquare = () => {
console.log(this.state);
this.setState({
square: !this.state.square,
line: !this.state.line
});
};
_handleLine = () => {
console.log(this.state);
this.setState({
line: !this.state.line,
dotted: !this.state.dotted
});
};