Terraya
Terraya

Reputation: 193

ReactJS Hooks Update an Array with another Array

i want to Update an empty array by another array, i cant realy figure out how todo that,

const [avatar, setAvatar] = useState([]);

and i want to update the empty array with another array like this:

PlayerService.getAvatars()
                    .then(response => {
                        setAvatar(avatar => [...avatar, response])
                    })

the problem here is, that the 4 items i get from the response , they will be set to the first element of the avatar array, not to multiple,

that means, i get under avatar[0] , all the items together, they dont split up like they should.

i hope you can follow me, im not a native english speaker

Upvotes: 1

Views: 908

Answers (1)

Arpit Vyas
Arpit Vyas

Reputation: 2227

you can concat array if you want to combine two array like this .

PlayerService.getAvatars()
    .then(response => {
        let arr1 = avtar;
        let arr2 = response;
        let arr3 = arr1.concat(arr2);
        setAvatar(arr3)
    })

Upvotes: 2

Related Questions