Skip to content

Instantly share code, notes, and snippets.

@yunusemredilber
Created December 19, 2020 15:44
Show Gist options
  • Save yunusemredilber/751963569d1f8137a0ec1bde07f03c95 to your computer and use it in GitHub Desktop.
Save yunusemredilber/751963569d1f8137a0ec1bde07f03c95 to your computer and use it in GitHub Desktop.
React spring animation example
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