Animated.View和Animated.Value引发只读分配或不可变对象错误

时间:2018-07-06 20:43:59

标签: react-native react-animated

我认为Animated.View和Animated.Value的用法非常简单,可以控制滑动打开的菜单栏的位置。我试图将Animated.timing()放在构造函数componentWillMount,componentDidMount中;这些选项均无效。打击配置的错误是:Attempted to assign readonly property

如果我将Animated.timing()移至构造函数,则会得到:you attempted to set the key on an object that is meant to be immutable.

这是我当前的代码:

import React, { Component } from 'react'
import {
  View,
  ScrollView,
  Text,
  StyleSheet,
  TouchableOpacity,
  Animated,
  Easing,
} from 'react-native'
import _style from '../../masterStyle'

export default class Nav extends Component {
  constructor(props){
    super(props);
    this.state = {
      navPosition: new Animated.Value(-230)
    };

    Animated.timing(this.state.navPosition, {
      toValue: 0,
      easing: Easing.back(),
    }).start(()=>{
      console.log("did start")
    });

  }

  componentDidMount(){
  }

  render(){
    return (
      <Animated.View style={style.navWrap}>
        <TouchableOpacity style={style.navMask} onPress={this.props.hide}>{}</TouchableOpacity>
        <ScrollView style={[style.menuWrap,{right: this.state.navPosition}]}>
          <Text style={_style.h2}>{}</Text>
        </ScrollView>
      </Animated.View>
    )
  }
}

const style = StyleSheet.create({
  navWrap: {
    flex: 1,
    position: 'absolute',
    width: '100%',
    height: '100%',
    top: 0,
    right: 0,
    zIndex: 10,
  },
  navMask: {
    position: 'absolute',
    top: 0,
    right: 0,
    width: '100%',
    height: '100%',
    backgroundColor: '#000',
    opacity: 0.8,
  },
  menuWrap: {
    position: 'absolute',
    flex: 1,
    width: 230,
    height: '100%',
    paddingTop: 0,
    backgroundColor: 'white',
    zIndex: 11,
    borderWidth: 0,
    borderColor: 'red',
    borderTopWidth: 60,
    borderTopColor: '#123357'
  },
  menuCloseIcon: {
    fontSize:30,
    fontWeight:'100',
    color: '#979797'
  },
});

0 个答案:

没有答案