TypeError: Invalid attempt to spread non-iterable instance
This is because it is a runtime error, not a "compile time" error.
Is there a line number associated with the error? Based on the question being about the spread operator I'll assume it's this line: newArticlesData=[...prevData,...response.data]
. I assume your prevData
is iterable, but is your response data? Try newArticlesData=[...prevData, response.data]
?
Here's an example of invalid spread operator use:
function trySpread(object) { let array; try { array = [...object]; console.log('No error', array); } catch(error) { console.log('error', error); }}
// errortrySpread({});trySpread({foo: 'bar'});trySpread(4);
// no errortrySpread([]);trySpread(['foobar']);trySpread('foobar');
Invalid attempt to spread non-iterable instance with django REST and Vue.js
It looks like the data
returned from the API doesn't have a property called results
which is undefined when you try to spread it (only arrays and object support that spread operator), so you should do :
this.reviews.push(...data)
React "TypeError: Invalid attempt to spread non-iterable instance"
You're forgetting to spread your new value for favoriteBeers
into an array:
setFavoriteBeers([
...favoriteBeers,
beers.find(beer => beer.id === id)
])
Related Topics
How to Add Spaces to for Loop Output in JavaScript
How to Fix Problem Closing Dropdown When I Click Outside It
How to Use Fetch Within a For-Loop, Wait for Results and Then Console.Log It
Show Form Fields Based on Selected Option
Javascript/Dom: How to Remove All Event Listeners of a Dom Object
Easiest Way to Mask Characters in Html(5) Text Input
Removing Currency Symbol and Replacing Comma With Point Using Pure JavaScript
How to Replace a String With Square Brackets Using JavaScript Replace Function
How to Add Disable Option or Default Option in Select Box
Chart.Js Showing Time (Hh:Mm:Ss - 24 Hour Clock) on Xaxis
How to Send a Http Delete Request from Browser
Prevent Particular Child Element from Firing Parent'S Mouseover Event in Jquery
Fullcalendar - Change View for Mobile Devices
Converting Excel Date Serial Number to Date Using JavaScript
How to Get the Timezone Name in JavaScript
Javascript Index of for Multiple Values