【发布时间】:2018-06-05 13:10:28
【问题描述】:
我正在使用react-native-navigation(不确定这是否相关)并尝试处理简单的道具。但是,当我从 ScreenA 转到 ScreenB 时,它可以正常工作。之后,如果我pop() screen 到 ScreenA 然后再次单击同一组件转到 ScreenB,应用程序崩溃并出现以下错误:
您试图将键
a设置为值2本来应该是不可变的并且已被冻结的对象。
这是ScreenA: Preview 中代码的最小示例:
import React, { Component } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import { navigationActions } from 'react-native-navigation';
export default class Preview extends Component {
constructor(props) {
super(props);
this.state = {
data: { a: 0, b: 1, c: 2 }
};
}
_goToFullview() {
let temp = this.state.data;
temp.a = 2;
this.setState({ data: temp });
this.props.navigator.push({ screen: "App.FullView", passProps: { data: this.state.data } });
}
render() {
return (
<View>
<Text>{"a: " + this.state.data.a + ","}</Text>
<Text>{"b: " + this.state.data.b + ","}</Text>
<Text>{"c: " + this.state.data.c}</Text>
<TouchableOpacity onPress={ () => this._goToFullview() }>
<Text>Go To Fullview</Text>
</TouchableOpacity>
</View>
)
}
}
这里是ScreenB: FullView:
import React, { Component } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import { navigationActions } from 'react-native-navigation';
class Fullview extends Component {
render() {
return (
<View>
<Text>{"a: " + this.props.data.a + ","}</Text>
<Text>{"b: " + this.props.data.b + ","}</Text>
<Text>{"c: " + this.props.data.c}</Text>
<TouchableOpacity onPress={ () => this.props.navigator.pop({ animated: true, animationType: 'fade' }) }>
<Text>Go back to preview</Text>
</TouchableOpacity>
</View>
)
}
}
我相信以上信息应该足以理解我的问题。谁能解释为什么会发生这种情况以及解决此问题的简单解决方案或提示?
【问题讨论】:
-
你从哪里得到
this.state.poll你所在的州没有任何投票变量 -
打错了,是
this.state.data
标签: react-native react-native-navigation