简体   繁体   English

我试图将一个对象添加到我存储在我的状态对象中的嵌套数组中,但似乎有问题

[英]I am trying to add an object to a nested array I have stored in my state object, but seem to have an issue with it

I have an object which I have created in state and it is my model for the booking. 我有一个我在州创建的对象,它是我预订的模型。 My addPassenger function should when click create an empty object into array with properties. 我的addPassenger函数应该在单击时使用属性创建一个空对象到数组中。 When I console.log the new array it returns me the array properly but when I execute function nothing happens, no error. 当我在console.log中新建数组时,它会正确返回数组但是当我执行函数时没有任何反应,没有错误。 Just blank console. 只是空白控制台。

My whole component https://pastebin.com/RLprKJrr 我的整个组件https://pastebin.com/RLprKJrr

 booking: {
        id: "",
        number: "",
        date: "",
        bookingStatus: "OPCIJA",
        paymentMethod: "MODELA1",
        tripID: 1,
        passengers: [
          {
            id: "",
            holder: {
              email: "",
              address: "",
              mobile: ""
            },
            name: "",
            surname: "",
            passport: "",
            idCard: ""
          }
        ]
      }

addPassenger = e => {
    e.preventDefault();
    let passengersArray = [...this.state.booking.passengers];
    console.log(passengersArray);
    this.setState({
      passengers: [...passengersArray, {
        id: "", 
        name: "",
        surname: "",
        passport: "",
        idCard: ""
      }]
    })
    this.checkArrayLength();
  }

Because the problem is with data being passed to setState 因为问题是将数据传递给setState

This is the structure of your initial state 这是您初始状态的结构

{
  booking: {
    passengers: {
      ...
    }
  }
}

Which is then accessible through this.state.booking.passengers 然后可以通过this.state.booking.passengers访问this.state.booking.passengers

But when you are setting state again, you are putting passengers on same level as bookings like this 但是当你再次设置状态时,你将乘客放在与这样的预订相同的水平上

{
   booking: {
     ...
   },
   passengers: {
     ...
   }
 }

So you basically need to do this 所以你基本上需要这样做

this.setState({
  bookings: {
    ...this.state.bookings,
    passengers: [...passengersArray, {
        id: "", 
        name: "",
        surname: "",
        passport: "",
        idCard: ""
      }]
  }
})

Please try to set state of booking.passengers, not just passengers: 请尝试设置booking.passengers的状态,而不仅仅是乘客:

this.setState({
  booking.passengers: ...

You have to include the key 'booking' in your new state: 您必须在新状态中包含“预订”键:

this.setState(previousState => ({
  booking: {
    ...previousState.booking,
    passengers: [...passengersArray, {
      id: "", 
      name: "",
      surname: "",
      passport: "",
      idCard: ""
    }]
  }
}));

I hope it helps you! 我希望它对你有所帮助!

暂无
暂无

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

相关问题 我试图写一个从数组创建一个简单的对象。 在我的数组中,我存储了变量: - I am trying to write a create a simple object from an array. In my array, I have variable stored: 我在react中有数组的平坦状态,我想使其成为嵌套数组并将其推到另一个对象ins状态 - i have flat state of arrays in react and i want to make it nested array and push it to another object ins state 我在将对象添加到数组时遇到问题 - I have problem with adding my object to array 我正在尝试将 object 添加到 object 中的数组末尾 - I am trying to add an object to the end of an array inside said object 我是否通过替换对象中的整个数组来改变我的reducer状态? - Am I mutating my reducer state by replacing an entire array in an object? 如何在我的状态中动态添加嵌套对象? - How can i add nested object inside my state dynamically? 如果我在对象中有一个数组并且该对象在一个大数组中,我该如何更新 React Native 状态? - How can I update the React Native state if I have an array inside an object and the object is inside in a big array? 我正在尝试使用javascript更改表单元格的属性,并且找到了我的建议,但似乎无法使它们正常工作 - I am trying to change the properties of a table cell with javascript and have found my suggestions but I can't seem to get them to work 我有一个JSON对象。 我想在Java服务中访问。 我正在尝试解析 - I have a json object. I want to access in java service. I am trying to parse it 从我的反应对象复制状态时,为什么我必须传播我的对象两次 - Why do I have to spread my object twice when copying state from my react object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM