如何解决设置对象内部变量的状态
constructor(props) {
super(props)
this.state = {
per_name:'',per_email:'',person:{
name:'',email:'',array:[]
}
}
}
this.setState({
person:{
name:this.state.per_name,email:this.state.per_email
}
})
如果我尝试以这种方式设置状态“未定义per_name”,我将做出新的反应,并引发错误。 在这种情况下应如何设置状态?
解决方法
您应该使用传播算子来保留未修改的密钥:
include ':app'
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(),'.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}
plugins.each { name,path ->
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
include ":$name"
project(":$name").projectDir = pluginDirectory
}
,
您可以按照此做法获得更多许可
constructor(props) {
super(props)
this.state = {
per_name:'',per_email:'',person:{
name:'',email:'',array:[]
}
}
}
someMethod() {
const {person,per_name,per_email} = this.state;
person.name = per_name;
person.email = per_email;
this.setState({
person
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。