简体   繁体   中英

React Component with onChange function, need to convert it using spread operator to include one more property

I have a component hooked up to Redux that needs to update the items and price in a cart component.

I tried to create this functionality by entering it in the action creator with Object.assign(item, { isSelected: true }) but it's breaking the update functionality for all of my checkboxes since it hard sets the isSelected property to true the first time.

I have an onChange function inside my actual component that's attached to my select drop downs where I need to pass in this functionality using a spread operator but I'm not sure how to do it.

Here is my current onChange function:

<StyledSelect
    value={selectedItem.id}
    onChange={e => {
        updateSelectedItem(e.target.value)
        props.updateCart({
            lineOne: 'Business',
            lineTwo: 'addOns',
            itemType: 'Item',
            item: items.configurationItems.find(
              obj => obj.id === e.target.value
            )
        })
    }}
>

Update Cart function as part of the action creator

export function updateCart({
  lineOne, lineType, itemType, item
}) {
  return {
    type: UPDATE_CART,
    payload: {
      lineOne,
      lineTwo,
      itemType,
      item
    }
  }
}

Any ideas?

Figured out what I needed to do. Moved that bit of logic from my Action Creator into the onChange itself. Code below for anyone's reference

<StyledSelect
            value={selectedEquipment.id}
            onChange={e => {
              updateSelectedEqupment(e.target.value)
              const item = items.configurationItems.find(
                obj => obj.id === e.target.value
              )
              Object.assign(item, { isSelected: true })
              props.updateCart({
                lineOne: 'Business',
                lineTwo: 'addOns',
                itemType: 'Items',
                item
              })
            }}
          >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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