Skip to content

Allow access to spring's "current goal value" through AnimatedValue.getGoalValue() #785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
guopengliang opened this issue Aug 13, 2019 · 2 comments
Labels
kind: request New feature or request that should be actioned

Comments

@guopengliang
Copy link
Contributor

guopengliang commented Aug 13, 2019

🚀 Feature Proposal

Allow access to the "current goal value" of an AnimatedValue.
EDIT: method renamed to getGoalValue, was getEndValue when proposed.

AnimatedValue.getGoalValue(); // the goal value that's currently animating to

Motivation

When writing more complicated animations, sometimes we have to share the same AnimatedValues across multiple/nested components. In addition to .getValue(), it would be super handy if we could also access the "current goal value" via .getGoalValue().

Example

Define a spring in Component A and pass it to Component B.

const [pos, set] = useSpring(() => ({ x: 0, y: 0 }));
set({ x: 331, y: 85 });

Access "current value" as well as "current goal value" from Component B.

pos.x.getValue(); // = 123.4567 being animating to 331
pos.x.getGoalValue(); // = 331
...
set({ x: 109 }); // update
...
pos.x.getValue(); // = 112.3456 being animated to 109 (previously to 331)
pos.x.getGoalValue(); // = 109
@guopengliang guopengliang added the kind: request New feature or request that should be actioned label Aug 13, 2019
@guopengliang guopengliang changed the title Allow access to spring's "current end value" through AnimatedValue.getEndValue() Allow access to spring's "current goal value" through AnimatedValue.getGoalValue() Aug 23, 2019
@aleclarson
Copy link
Contributor

In 9.0.0-canary.809.2.49361198 (a canary version for #808 and #809), you can do the following:

const style = useSpring({ x: props.x })
style.x.get() // Get current value.
style.x.animation.to // Get goal value.

@aleclarson
Copy link
Contributor

Since 9.0.0-canary.808.13.719834d, you can use the goal property instead.

style.x.goal

Using .goal is better than .animation.to for two reasons:

  • Less typing
  • If your spring is animating toward another spring, .goal will return the current value of the target spring, while .animation.to will return the target spring
const {x} = useSpring({ x: props.x })
const {y} = useSpring({ y: x })
y.animation.to === x
y.goal === props.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: request New feature or request that should be actioned
Projects
None yet
Development

No branches or pull requests

2 participants