简体   繁体   English

ReactJS如何更改状态中存储的对象的值

[英]ReactJS How to change value of object stored in state

All code will be below my question and explanation. 所有代码均在我的问题和解释下方。

So what I am trying to do is update my state which is in a grandparent class, this is done for reasons to do with files, I am also using Material-UI for the text boxes. 所以我想做的是更新祖父母类中的状态,这样做是出于处理文件的原因,我也在文本框中使用了Material-UI。 And I'm also writing Redux without Redux, Because I can just if you see some strange things. 而且我也在编写没有Redux的Redux,因为我可以看到一些奇怪的东西。

My grandparent class state looks like this: 我的祖父母的上课状态如下所示:

state = {
  value: 0,
  application: {
    ApplicationPK: '',
    Person: [
      {
        PersonPK: '',
        FullName: '',
        TitleFK: '',
        Forename: '',
        MiddleNames: '',
        Surname: '',
        DateOfBirth: '',
        HomeTelephone: '',
        MobileTelephone: '',
        EmailAddress: '',
        LoanPurposeFK: '',
        MaritalStatusFK: '',
        Addresses: [
          {
            FlatNumber: '',
            BuildingName: '',
            BuildingNumber: '',
            Street: '',
            District: '',
            Town: '',
            County: '',
            Postcode: '',
            ResidentialStatusFK: '',
            DateMovedIn: '',
            IsCurrentAddress: '',
            AddressPK: '',
          },
        ],
        Employment: [
          {
            EmploymentStatusFK: '',
            Employer: '',
            JobTitle: '',
            Telephone: '',
            MonthlyPay: '',
            IsPaidByBACS: '',
            PayFrequencyFK: '',
            DayOfMonth: '',
            DayOfWeek: '',
            NextPayDate: '',
            FollowingPayDate: '',
            DateStarted: '',
            Postcode: '',
            EmploymentPK: '',
            Website: '',
          },
        ],
        BankAccount: [
          {
            Name: '',
            Sortcode: '',
            AccountNumber: '',
            IsAccountHolder: '',
            IsJointHolder: '',
            IsSoleAuthoriseDebits: '',
            IsPrimary: '',
            IsActive: '',
            BankAccountPK: '',
          },
        ],
      },
    ],
  },
};

I know its long but thats because it errors because mui text boxes dont like null values on load. 我知道它的时间很长,但是那是因为它出错,因为Mui文本框不喜欢加载时的空值。

this is how im changing the state at the moment but its adding it as a value at the top level of the state and i want it to obviously replace the value that is inside person. 这就是即时消息如何更改状态,但是将其作为状态的顶级值添加到状态,我希望它明显替换人内部的值。 this function is in the same class as the state obviously 此功能与状态明显在同一类中

 handleChangeType = event => {
   this.setState({ [event.target.name]: event.target.value });
 }

and Finally the Mui Text Box looks like this: 最后,Mui文本框如下所示:

<Input
   defaultValue={this.props.state.application.Person[0].Forename}
   className={classes.input}
   onChange={this.props.handleChangeType}
   name="Forename"
   inputProps={{
     'aria-label': 'Description',
   }}
/>

TL:DR: How do i update the correct value in the state TL:DR:如何在状态下更新正确的值

I know its long but thats because it errors because mui text boxes dont like null values on load. 我知道它的时间很长,但是那是因为它出错,因为Mui文本框不喜欢加载时的空值。

To avoid initializing a bunch of empty strings everywhere, you could just do: 为了避免在各处初始化一堆空字符串,您可以执行以下操作:

defaultValue={((this.props.state.application || {}).Person[0] || {}).Forename || ""}

As per updating the correct state variable, I would use a different handler for each category. 根据更新正确的状态变量,我将为每个类别使用不同的处理程序。 You must also bear in mind that your state variable here is the Person array. 您还必须记住,这里的状态变量是Person数组。 If you want to update anything within it, you need to pass a new array to the state. 如果要更新其中的任何内容,则需要将新数组传递给状态。 Finally, you seem to want to be able to have multiple users inside your Persons array, but your event handlers aren't given any info of which user you want to update field X or Y. 最后,您似乎希望在Persons数组中可以有多个用户,但是没有为事件处理程序提供要更新字段X或Y的任何用户的任何信息。

For example: 例如:

handleChangeType = (userIndex, name, event) => {
  let person = Object.assign({}, this.state.Persons[userIndex]);  //shallow-copy
  person[name] = event.target.value
  this.setState({Person: person });
}

For each object or array that's nestled, you'll need to create a copy, change the copy and then replace the original with the copy. 对于嵌套的每个对象或数组,您需要创建一个副本,更改副本,然后用副本替换原始对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM