Вы можете сделать это, используя правило @keyframes
CSS Animation
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
show: true,fadeOut: true };
}
toggle = () =>{
this.setState({ fadeOut: !this.state.fadeOut })
if(this.state.show === false){
setTimeout(() => {
this.setState({ show: !this.state.show })
}, 1000)
}else{
this.setState({ show: !this.state.show })
}
}
render() {
return (
<div>
<button onClick={this.toggle}>Click</button>
{this.state.show ? null : <div className={this.state.fadeOut ? 'fadeOut' : 'fade'}><Child /></div>}
</div>
);
}
}
class Child extends React.Component {
render() {
return (
<div className="fade">
<h1>Fade In/Out Using CSS </h1>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
.fade {
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari and Chrome */
-o-animation: fadein 2s; /* Opera */
}
.fadeOut{
animation: fadeout 2s;
-moz-animation: fadeout 2s; /* Firefox */
-webkit-animation: fadeout 2s; /* Safari and Chrome */
-o-animation: fadeout 2s; /* Opera */
}
@keyframes fadein {
from {
opacity:0;
}
to {
opacity:1;
}
}
@-moz-keyframes fadein { /* Firefox */
from {
opacity:0;
}
to {
opacity:1;
}
}
@-webkit-keyframes fadein { /* Safari and Chrome */
from {
opacity:0;
}
to {
opacity:1;
}
}
@-o-keyframes fadein { /* Opera */
from {
opacity:0;
}
to {
opacity: 1;
}
}
@keyframes fadeout {
from {
opacity:1;
}
to {
opacity: 0;
}
}
@-moz-keyframes fadeout { /* Firefox */
from {
opacity:1;
}
to {
opacity: 0;
}
}
@-webkit-keyframes fadeout { /* Safari and Chrome */
from {
opacity:1;
}
to {
opacity:0;
}
}
@-o-keyframes fadeout { /* Opera */
from {
opacity:1;
}
to {
opacity: 0;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='root'></div>