Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

For some reason I'm not able to see what I'm doing wrong with my code. I seem to be using Animated just as the documentation shows but this error keeps coming. The code snippet:

import React, {
  Component
} from 'react';
import {
  StyleSheet,
  Image,
  Animated,
} from 'react-native'
import Header from './../components/Header'


export default class DrawerShell extends Component {
  constructor(props) {
    super(props)
    this.state = {
      showNav: false,
    }
    this.anim = new Animated.Value(0)
    this.openDrawer = this.openDrawer.bind(this)
  }
  openDrawer() {
    let toValue
    this.setState({
      showNav: !this.state.showNav
    }) !this.state.showNav ? toValue = 1 : toValue = 0
    Animated.timing( // Animate value over time
      this.anim, // The value to drive
      {
        toValue: 1, // Animate to final value of 1
        duration: 300,
      }
    ).start()
  }
  render() {
    let {
      showNav
    } = this.state
    return ( <
      Animated.View style = {
        [
          styles.appContainer,
          {
            transform: [{
                translate: [
                  this.anim.interpolate({
                    inputRange: [0, 1],
                    outputRange: [0, 200],
                  }),
                  this.anim.interpolate({
                    inputRange: [0, 1],
                    outputRange: [0, 80],
                  }),
                  0
                ]
              },
              {
                scale: this.anim.interpolate({
                  inputRange: [0, 1],
                  outputRange: [1, 0.7]
                })
              }
            ]
          },
        ]
      } >
      <
      Image source = {
        {
          uri: "splash_bg"
        }
      }
      style = {
        styles.bgImage
      } >
      <
      Header title = "hi there"
      onPress = {
        this.openDrawer
      }
      /> <
      /Image> 
     </Animated.View>
    );
  }
}

enter image description here

like image 440
Jeff P Chacko Avatar asked Sep 17 '17 18:09

Jeff P Chacko


1 Answers

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.

like image 184
fionbio Avatar answered Nov 15 '22 09:11

fionbio