Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,471 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