How to fade in an element when a property changes ?
I'd like the element returned by the statusMessage()
function to fade in each time the this.props.statusMessage
changes.
Currently no animations are applied. It doesn't appear as though any classnames are added either.
class SelectPlayer extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
selectedId = this.props.selectedId;
selectedPlayerName = this.props.selectedPlayerName;
Store.dispatch(Actions.updateScore(selectedId, selectedPlayerName));
}
statusMessage() {
return (
<ReactCSSTransitionGroup
transitionName='message'
transitionAppear={true}
transitionAppearTimeout={2000}
transitionEnterTimeout={500}
transitionLeaveTimeout={500}>
<div key="1">{this.props.statusMessage.text}</div>
</ReactCSSTransitionGroup>
)
}
render() {
if (this.props.selectedPlayerName) {
return (
<div className="details">
<div className="name">{this.props.selectedPlayerName}</div>
<button className="inc" onClick={this.handleClick}>
Add 5 points
</button>
{ this.statusMessage() }
</div>
);
}
else {
return (
<div className="message">Click a player to select</div>
);
}
}
};
CSS
.message {
line-height: 2.25rem;
text-align: center;
}
.message-appear {
opacity: 0.01;
}
.message-appear.message-appear-active{
opacity: 1;
}
You can use CSS classes to animate in React Let's start by creating our React component first. import React, { useState } from 'react'; import classNames from 'classnames'; import styles from './App. module. css'; const App = () => { const [animate, setAnimate] = useState(false); const handleClick = () => setAnimate(!
CSSTransition example: Transition a React component using CSS. The CSSTransition component allows you to apply transitions to elements entering and leaving the DOM using CSS. You can achieve this by using the following props: in , a Boolean value used to control the appearance of the element.
React Transition Group is a good animation library and has a very small bundle size. It's one of the most popular animation libraries and should be considered for your next React project.
This might be easier with react-animate-on-change (disclosure: I'm the author).
In render you would do something like this:
render() {
if (this.props.selectedPlayerName) {
return (
<div className="details">
<div className="name">{this.props.selectedPlayerName}</div>
<button className="inc" onClick={this.handleClick}>
Add 5 points
</button>
<AnimateOnChange
baseClassName="message"
animationClassName="message-clicked"
animate={this.props.selectedId === this.props.id}>
{this.props.statusMessage.text}
</AnimateOnChange>
</div>
);
}
else {
return (
<div className="message">Click a player to select</div>
);
}
}
And then use animation in your CSS:
.message-clicked {
animation: clicked-keyframes 1s;
}
@keyframes clicked-keyframes {
from {opacity: 0.01}
to {opacity: 1}
}
It's hard to imagine how you want the result to be. But to the sounds of it, you have a player list which can be clicked and then a view of selected players? In that case, you should rather use an array with selected players and use ReactCSSTransitionGroup to render players entering/leaving the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With