Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript React by Example Building the Application Adding Guests to the List

setState not persisting when page is refreshed

I'm able to add a name to the array, and I can see the array length change in the dev tools, but when I refresh the page, the name I added disappears from the guest array. Has anyone else had this problem?

2 Answers

Matthew Lang
Matthew Lang
13,483 Points

When you refresh your page, you are essentially re-loading your app from scratch. That means, all data you added will be refreshed unless you store it somewhere and load it when your application begins.

Oh of course! That was a silly question. Thanks for answering!

refreshing the page cause the app to get back to its initial state (your code),however if you're curious about how you can save the state, one solution is to use localStorage, so whenever a change happens to the state you save it to the localstorage and when your app loads grab the stored state in localstorage and assign it to app's state and then your data will be available even when refreshing. hope that helps you