24
loading...
This website collects cookies to deliver better user experience
useSpring
hook.useSpring
hook you to animate an element's style by controlling its style from when first appears to when the animation completes:// some-component.js
import { animated, useSpring } from 'react-spring';
const spring = useSpring({
from: { opacity: 0 },
to: { opacity: 1 },
});
// ...
<animated.div style={spring}>Hello World</animated.div>
{ from, to, ...etc }
config (the argument that useSpring
takes) with an animation name:// animations.js
export default {
fadeIn: {
from: { opacity: 0 },
to: { opacity: 1 },
},
};
// some-component.js
import { animated, useSpring } from 'react-spring';
import animations from './animations';
const spring = useSpring(animations.fadeIn);
// ...
<animated.div style={spring}>Hello World</animated.div>
react-spring
modules plus our animations
object from a single file:// animations.js
export const animations = {
fadeIn: {
from: { opacity: 0 },
to: { opacity: 1 },
},
};
export * from 'react-spring';
// some-component.js
import { animated, animations, useSpring } from './animations';
const spring = useSpring(animations.fadeIn);
// ...
<animated.div style={spring}>Hello World</animated.div>
animated
, animations
, and useSpring
, and then scope animations.fadeIn
to useSpring
.use[AnimationName]
hooks that return all that we need:// animations.js
import { animated, useSpring } from 'react-spring';
const animations = {
fadeIn: {
from: { opacity: 0 },
to: { opacity: 1 },
},
};
export function useFadeIn() {
const spring = useSpring(animations.fadeIn);
return {
animated,
spring,
};
}
// some-component.js
import { useFadeIn } from './animations';
const { animated, spring } = useFadeIn();
// ...
<animated.div style={spring}>Hello World</animated.div>
useSpring
wrapper:// animations.js
import { animated, useSpring as useBaseSpring } from 'react-spring';
const animations = {
fadeIn: {
from: { opacity: 0 },
to: { opacity: 1 },
},
};
export const PRESETS = Object.freeze(Object.keys(animations));
export function useSpring({ preset } = {}) {
const spring = useBaseSpring(animations[preset]);
return {
animated,
spring,
};
}
// some-component.js
import { PRESETS, useSpring } from './animations';
const { animated, spring } = useSpring({ preset: PRESETS.fadeIn });
// ...
<animated.div style={spring}>Hello World</animated.div>
useSpring
hook.