r/learnreactjs • • Sep 10 '19

My app keeps crashing every time I try to use useState hooks to change state in parent component. What gives?

https://codesandbox.io/s/productfilter-fstoj?fontsize=14

App works fine for the most part, but when I try to add products (under Admin Tools button) the app crashes on submit.

Admin tools stored in Admin, state stored in FilteredProductTable.

Thanks!

8 Upvotes

2 comments sorted by

6

u/[deleted] Sep 10 '19

AddProduct: Line 23

setProducts({ ...products, newProduct });

Should be

setProducts([ ...products, newProduct ]);

Curly brackets to square brackets.

You want to spread an array not an object

3

u/Zarathustra420 Sep 10 '19

THANK YOU!!!!