I'm trying to figure out how to trigger the animation only once at the startup of the app..
Currently I'm facing the problem that If I switch to another view(another component, using React-router-dom
) and then switch back to Home component, animation fires again. I'm using react-spring for this animation.
I just want to make that animation fire only once on start of the app. Not after every switch to Home. Is there any config in React-spring, or do you guys know how to prevent this? I can't really find anything helpful.
Setting the animation
//State for animation
const [showGameOptions, setShowGameOptions] = useState(true);
//Starting animation
const gameOptionsTransitions = useTransition(showGameOptions, null, {
from: { opacity: 0, transform: 'translateY(200px)' },
enter: { opacity: 1, transform: 'translateY(0)' },
leave: { opacity: 0, transform: 'translateY(200px)' },
});
I've set the initial state to true, because I want to trigger that animation on start. To this part it works nice.
Home Component's return()
<div className="homeScreen">
<h1>Tic Tac Toe</h1>
<p>Select game mode</p>
{gameOptionsTransitions.map(
({ item, key, props }) =>
item && (
<animated.div key={key} style={props}>
<Link to="/play" className="setGameMode-btn" onClick={playPlayerVsComputer}>
Player vs. Computer
</Link>
<Link to="/play" className="setGameMode-btn" onClick={playPlayerVsPlayer}>
Player vs. Player
</Link>
</animated.div>
),
)}
</div>