Insert an item in a list inside another in react state

Ask a question
2

I have this variable on my state:

this.state = {
        itemList: {
            item: [
                {
                    itemAlias: [
                        {
                            name: null
                        }
                    ],
                    idItem: null,
                    itemName: null,
                }
            ]
        }
    }

what I want to do is to insert a new item alias on a cetain item.I did this and its inserting a new itemAlias to my item but is also creates a new Item, and I dont want that, I just want to update my item with a new Item alias:

insertAliasToList = (itm) => {
    let insertedAlias = {
        name: 'test'
    }

    itm.itemAlias.push(insertedAlias)

    this.setState(prevState => ({
        ...prevState,
        itemList: {
            ...prevState.itemList,
            item: [...prevState.itemList.item, p]
        }
    }))
}

I have also tried this but the error Uncaught TypeError: Invalid attempt to spread non-iterable instance appears.

insertAliasToList = (itm) => {
    let insertedAlias = {
        name: 'test'
    }

   // itm.itemAlias.push(insertedAlias)

    this.setState(prevState => ({
        ...prevState,
        itemList: {
            ...prevState.itemList,
            item: {
                ...prevState.itemLIst.item,
                itemAlias:[...prevState.itemList.item.itemAlias,insertedAlias]
            }
        }
    }))
}
Coin iconoffer bounty
add comment

0 Answer

Your Answer