[英]react native “attempt to set value to an immutable object”
I'm creating a draggable box.我正在创建一个可拖动的框。 which I can drag anywhere on the screen but I'm getting this error which says that " You attempted to set the key
_value
on an object that is meant to be immutable and has been frozen ".我可以将其拖动到屏幕上的任何位置,但我收到此错误消息,提示“您试图在一个不可变且已被冻结的对象上设置键
_value
”。 Can anyone tell me what am I doing wrong.谁能告诉我我做错了什么。
My Code:我的代码:
import React, { Component } from 'react'
import {
AppRegistry,
StyleSheet,
Text,
Button,
ScrollView,
Dimensions,
PanResponder,
Animated,
View
} from 'react-native'
import { StackNavigator } from 'react-navigation'
export default class Home extends Component{
componentWillMount(){
this.animatedValue = new Animated.ValueXY();
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onPanResponderGrant: (e, gestureState) => {
},
onPanResponderMove:Animated.event([
null,{dx: this.animatedValue.x , dy:this.animatedValue.y}
]),
onPanResponderRelease: (e, gestureState) => {
},
})
}
render(){
const animatedStyle = {
transform:this.animatedValue.getTranslateTransform()
}
return(
<View style={styles.container}>
<Animated.View style={[styles.box ,animatedStyle]} {...this.panResponder.panHandlers}>
<Text>Home</Text>
</Animated.View>
</View>
)
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
marginLeft: 10,
marginRight: 10,
alignItems: 'stretch',
justifyContent: 'center',
},
box:{
height:90,
width:90,
textAlign:'center'
}
});
就我而言,我收到此错误是因为我忘记将View
更改为Animated.View
。
Try this out.试试这个。 This will solve your issue.
这将解决您的问题。 You need to initialize animatedValue in state object to make it work.
您需要在 state 对象中初始化 animationValue 以使其工作。
constructor(props) {
super(props);
this.state = {
animatedValue: new Animated.ValueXY()
}
}
onPanResponderMove:Animated.event([
null,{dx: this.state.animatedValue.x , dy:this.state.animatedValue.y}
]),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.