2

How to use the useSpring() hook?

I'm trying to use the useSpring() hook to animate the transform property:

It simply doesn't work if the initial state is "translate3d(0,0,0)", for instance, if I initialise it like that with toggle being false:

const props = useSpring({
  transform: toggle ? "translate3d(0,-25px,0)" : "translate3d(0,0,0)"
});

This, on the other hand, works:

const props = useSpring({
  transform: toggle ? "translate3d(0,-25px,0)" : "translate3d(0,1px,0)"
});

Is this a bug? Thanks

zok
  • 6,065
  • 10
  • 43
  • 65

1 Answers1

8

You have to explicitly indicate unit of change. Like pixel or percentage. Try this:

const props = useSpring({
  transform: toggle ? "translate3d(0,-25px,0)" : "translate3d(0,0px,0)"
});
user9408899
  • 4,202
  • 3
  • 19
  • 32