I seem to lack fundamental understanding of how React Transition Group works. In this simple example, I expect the component to fade in on each update. It does not. Why is that the case? If I toggle the in prop for , then it does work.
import React from 'react';
import ReactDOM from 'react-dom';
import { Container, Button, Alert } from 'react-bootstrap';
import { CSSTransition } from 'react-transition-group';
import './styles.css';
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
rerender: false,
};
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState((state) => ({
rerender: !state.rerender,
}));
}
componentDidMount() {
console.log('Mounted');
}
componentDidUpdate() {
console.log('Updated');
}
render() {
return (
<div>
<Button onClick={this.toggle}>Click</Button>
<CSSTransition
classNames="fade"
in = {true}
timeout={1000}
>
{this.state.rerender ? <p> Render On </p> : <p>Render Off</p>}
</CSSTransition>
</div>
);
}
}
ReactDOM.render(
<Example />,
document.getElementById('root')
);
Here is the CSS
.fade-enter {
opacity: 1;
}
.fade-enter-active {
opacity: 0;
transition-property: opacity;
transition-duration: 1000ms;
}
.fade-enter-done {
opacity: 0;
}
From what I route read, I thought that leaving the "in" prop for as always true, will always apply the enter-* CSS classes on each component update. As you can see though, when I press the button, hence updating the component. No animation takes place: https://codesandbox.io/s/beautiful-brahmagupta-6nmze?file=/index.js On the other hand, if I toggle the in prop with true and false, then the entering classes will be applied. Try it in the code sandbox and see for yourself.
...
<CSSTransition
classNames="fade"
in = {this.state.rerender}
timeout={1000}
>
...