question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

animated doesn't work without forceUpdate for 2nd animation

See original GitHub issue

Description

I have a component and everytime the prop data is changed I want to animate the component in order to adjust it to the new height. The Animated.View gets an Value as a height style prop, but when I change the Value after componentDidUpdate the Value is not updated, unless I call forceUpdate and then it works.

Code

function runTiming(clock: Animated.Clock, startValue: Animated.Value<number>, destValue: Animated.Value<number>, duration: number) {
  const state = getInitialState(startValue);
  const config = getConfig(destValue,duration);

  return block([
    cond(clockRunning(clock), 0, [

      set(state.finished, 0),
      set(state.time, 0),
      set(state.position, startValue),
      set(state.frameTime, 0),
      set(config.toValue, destValue),
      startClock(clock),
    ]),
    timing(clock, state, config),
    cond(state.finished, stopClock(clock)),
    state.position,
  ]);
}

export class Component extends PureComponent<PropsType> {

  heightAnimation: any;

  constructor(props: PropsType) {
    super(props);
    this.heightAnimation = new Animated.Value(0);
    this.animate();
  }

  componentDidUpdate(prevProps: PropsType) {
    if (propsChangedAndNeedToUpdateHeight) {
      this.animate()
    }
  }

  animate = () => {
    this.heightAnimation = runTiming(new Clock(), new Animated.Value(oldHeight), new Animated.Value(destHeightValue), 400);
    this.forceUpdate(); //I want to delete this line
  }

  render() {
      return (
        <Animated.View style={{
          height: this.heightAnimation
        }
        }>
          {MyComponent}
        </Animated.View>
      );
  }
}

Package versions

  • React:
  • React Native:
  • React Native Reanimated:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
noobvimcommented, May 5, 2020

We know we can initialize an animation based on props. All we’re looking to do is update an animation based on props. The same thing we’re able to do everywhere else in React.

1reaction
jakub-gonetcommented, Jul 9, 2020

To update Animated.Value you need to use .setValue as @terrysahaidak suggested. forceUpdate detaches and reattaches nodes in the native side, that’s why it’s working.

Read more comments on GitHub >

github_iconTop Results From Across the Web

My animation is not working when re rendering my react ...
My animation is not working when re rendering my react component? · 2. After React rerender component it compares new render with the...
Read more >
Animations not playing & require a force update
We recently noticed an issue appear in our game where animations for player mounts were not playing. We noticed that they would play...
Read more >
Animation transitions
Use the following properties to adjust the transition and how it blends between the current and next state. Property, Function. Has Exit Time,...
Read more >
Working with Modular Characters
Describes the different methods you can use to create modular characters comprised of multiple Skeletal Meshes.
Read more >
How to Force Update a React Component
When you find yourself needing this method, you should first try to analyze your code and figure out if there is another reason...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found