Diarrah
Diarrah

Reputation: 125

How do I populate an array inside of an object with React Hooks

I have an object that has an array with it. Something like this:

const [data, setData] = useState({ jobs: [] });

Now I am fetching data from an API & I need to add that fetched information to the jobs array. How do I do that? When I do this: setData(newData) it is not going into that jobs array.

Please help!

Upvotes: 0

Views: 109

Answers (1)

jens
jens

Reputation: 2145

You need to wrap the result in an object and use then jobs key.

setData({ jobs: newData })

Upvotes: 1

Related Questions