animated doesn't work without forceUpdate for 2nd animation
See original GitHub issueDescription
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:
- Created 3 years ago
- Reactions:2
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.