error: Attempted to assign to read only property on using Animated react native

左心房为你撑大大i 提交于 2019-12-05 21:25:18

Might be useful for others coming from Google. Be sure you're using animated values within animated components like Animated.View. Often overlooked when 'upgrading' a view to be animated.

Figured. Two reasons why this error was being thrown.

  1. I was interpolating the same value multiple times. Which is not allowed.
  2. Setting state would call interpolate once more. Which was not required.

Once I Stopped doing interpolate multiple times on the same value and made it independent of state, the error went away.

For those who come here, you need to have animated values inside <Animated.View>!

Refer to this issue: https://github.com/facebook/react-native/issues/10716#issuecomment-258098396

i think this might work you assign value directly to the state object in returnary operator and that case the error

openDrawer() {
  let toValue
  this.setState({
    showNav: !this.state.showNav
  }) 
  toValue = (!this.state.showNav) ? 1 : 0 // change this this line case error
  Animated.timing( // Animate value over time
    this.anim, // The value to drive
    {
      toValue: 1, // Animate to final value of 1
      duration: 300,
    }
  ).start()
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!