react-native - react-native 中的对象不可变
问题描述
我必须传递对象选项来创建套接字连接,它的创建和使用如下:
var options = {port: 4444, host: this.props.ip, reuseAddress: true};
var client = TcpSocket.createConnection(options);
但我收到以下错误:
You attempted to set the key 'host' with the value x.x.x.x on an object that is meant to be inmutable and has been frozen.
如何克隆或复制 ip(由子组件作为道具接收)所以我不会收到此错误?
编辑:我已经尝试过了,但出现了同样的错误。
var self = this;
var clone = Object.assign({}, self.props)
var options = {port: 4444, host:clone.ip, reuseAddress: true};
解决方案
我必须创建 2 个套接字,如果我使用相同的选项对象,它会引发该错误。复制对象并将不同的对象传递给第二个套接字解决了这个问题。