A
A
Alexey Yakovlev2020-11-21 15:18:48
React
Alexey Yakovlev, 2020-11-21 15:18:48

Doesn't push into an array?

The user has:

boards: [
 0: {title: "trello-clone", background: "rgb(205 90 145)", cards: [{title: "3"}]}
]


cards- this is an array in boards, I need to push there, but the array is simply updated.

I get the user, an array for cards
const user = JSON.parse(localStorage.getItem('user'));
const [card, setCard] = React.useState(user.boards.map(item => item.cards));


function where i add to array:
function createCard() {
        user.boards.map(item => {
            item.cards = [];
        })

        user.boards.map(item => {
            item.cards.push({title: titleCard});
        })

        setTitleCard('');

        localStorage.setItem('user', JSON.stringify(user));
    
        setCard(user.boards.map(item => item.cards));
    }


As a result, if I wrote 1, for example, it was added then I wrote 2, then 1 will be erased, and 2 will remain, and so on.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mario Kun, 2020-11-21
@aleshaykovlev

Maybe something like this?

function createCard() {
 const item.cards = [];

       user.boards.map(item => {
            item.cards.push({title: titleCard});
        })

        setTitleCard('');

        localStorage.setItem('user', JSON.stringify(user));
    
        setCard(user.boards.map(item => item.cards));
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question