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.

[2.3] Toggling `withTiming` on/off doesn't animate

See original GitHub issue

Description

This code never animates:

const style = useAnimatedStyle(() => ({
  width: isActive.value ? 100 : withTiming(300)
}))

Instead, it always snaps directly between 100 and 300, without an animation.

Expected behavior

When active, this should snap to 100. When inactive, it should do a timing animation to 300. This worked on 2.2.

Actual behavior & steps to reproduce

The animation snaps to 300 when inactive. It doesn’t animate.

This solves it:

const style = useAnimatedStyle(() => ({
  width: withTiming(isActive.value ? 100 : 300, { duration: isActive.value ? 0 : 250 })
}))

But it shouldn’t be necessary.

Snack or minimal code example

View Code
import React from 'react'
import { Pressable, StyleSheet } from 'react-native'
import Animated, {
  useAnimatedStyle,
  useSharedValue,
  withTiming,
} from 'react-native-reanimated'

export default function HelloWorld() {
  const pressed = useSharedValue(false)
  const style = useAnimatedStyle(() => {
    return {
      // this never animates
      width: pressed.value ? 100 : withTiming(300),
      // this does animate
      // width: withTiming(pressed.value ? 100 : 300, { duration: pressed.value ? 0 : 250 })
    }
  })

  return (
    <Pressable
      onPressIn={() => (pressed.value = true)}
      style={styles.container}
      onPressOut={() => (pressed.value = false)}
    >
      <Animated.View style={[styles.shape, style]} />
    </Pressable>
  )
}

const styles = StyleSheet.create({
  shape: {
    justifyContent: 'center',
    height: 250,
    width: 250,
    borderRadius: 25,
    marginRight: 10,
    backgroundColor: 'black',
  },
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    flexDirection: 'row',
    backgroundColor: '#9c1aff',
  },
})

Video

https://www.loom.com/share/42aa2a930b0f440692cc6d1e2d86375b

Package versions

  • React Native: 0.63.4
  • React Native Reanimated: 2.3.2
  • NodeJS:
  • Xcode:
  • Java & Gradle:

Affected platforms

  • Android
  • iOS
  • Web

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
piaskowykcommented, Dec 23, 2021

Thanks for the report 🙌 I resolved the issue in linked PR.

1reaction
cam-shawcommented, Jan 20, 2022

@piaskowyk Do you plan to release a new patch version soon? This was a hard regression for us when we updated from v2.2.

Same here. This has prevented us from upgrading, but we’re also facing an issue where measure doesn’t work in iOS modals in 2.2, but is fixed in 2.3…

Read more comments on GitHub >

github_iconTop Results From Across the Web

withTiming | React Native Reanimated - Software Mansion
Returns​ This method returns an animation object. It can be either assigned directly to a Shared Value or can be used as a...
Read more >
HydroDAS & MiniDAS Oceanographic Data Acquisition Systems ...
allows it to operate at somewhat higher data rates, but it does not work well ... configuration, turning On/Off power outputs & serial...
Read more >
MCIMX6QP7CVT8AB MCIMX6DP7CVT8AB NXP USA Datasheet
The temperature read out does not reflect case or ambient temperature. ... keeping real time and other data on OFF state. ... would...
Read more >
u-blox 6 Receiver Description and Protocol Specification - StudyLib
2 2.3 Navigation Output Filters . ... 18 9.3.1.1 ON/OFF operation - long update period . ... The grid offset does not work...
Read more >
ZigBee Cluster Library (for ZigBee 3.0) User Guide - NXP
15.2 On/Off Switch Config Cluster Structure and Attribute ... Section 2.3.2) - if the attribute does not exist on the.
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