Created
December 19, 2020 15:44
-
-
Save yunusemredilber/751963569d1f8137a0ec1bde07f03c95 to your computer and use it in GitHub Desktop.
React spring animation example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { render } from 'react-dom' | |
import React, { useState } from 'react' | |
import { useSprings, animated } from 'react-spring' | |
const items = ['hey', 'yooo'] | |
function ReactSpringExample() { | |
const [pos, setPos] = useState(0) | |
const springs = useSprings(items.length, items.map((item, i) => ({ | |
opacity: pos === i ? 1 : 0, | |
config: { mass: 5, tension: 500, friction: 80 } | |
}))) | |
const next = () => { | |
setPos(pos => pos + 1) | |
} | |
const prew = () => { | |
setPos(pos => pos - 1) | |
} | |
return ( | |
<div> | |
{springs.map((props, i) => | |
<animated.div key={i} style={{ | |
display: props.opacity | |
.interpolate(o => o === 0 ? 'none' : 'block'), | |
...props | |
}}> | |
{items[i]} | |
</animated.div>) | |
} | |
<button onClick={prew}>prew</button> | |
<button onClick={next}>next</button> | |
</div> | |
) | |
} | |
render(<ReactSpringExample />, document.getElementById('root')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment