设置对象内部变量的状态

时间:2020-09-03 19:17:14

标签: javascript arrays reactjs object

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”,我将做出新的反应,并引发错误。 在这种情况下应如何设置状态?

2 个答案:

答案 0 :(得分:3)

您应该使用传播算子来保留未修改的密钥:

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
}

答案 1 :(得分:1)

您可以按照此做法获得更多许可

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
   });
}