Persist cart on refresh#58
Open
mattpauls wants to merge 7 commits intodeveloper-job-simulation:mainfrom
Open
Conversation
sbmsr
reviewed
Mar 10, 2023
| // Trying to have one location to update the local storage when the cart state changes, but only when items are added or removed. | ||
| // It doesn't make sense to overwrite the localstorage every time the page is loaded with the logic I have with state, above. | ||
| // But this implementation isn't much better, since I'm reading from disk twice on first load, and then again every time the cart is updated. | ||
| // The alternative that I'm aware of is to put it in the onClick actions for adding and removing from the cart, which probably makes more sense at this point in time. |
Member
There was a problem hiding this comment.
You're on the right track - linking the actions that update the cart to localStorage is what I would recommend, but with a slight twist.
instead of spreading the code to multiple components (as you stated), you can simply wrap the updateCart function in a new function. This new function can update the cart state, and also pass the new cart state straight into localStorage. Here is an example.
Author
There was a problem hiding this comment.
Ohhh I think I see what you did there. That makes sense, I didn't think of handling state within another function. Thank you!
Member
|
Hi @mattpauls 👋 I see you've completed 3/5ths of the issues. Let me know if you need any help! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses #2
I'd love some feedback on this one, if possible. I wasn't sure if I should put updating localStorage in an onClick() or whether to use a useEffect. Using the onClick method, at least the way I was implementing it, would mean I'd have to have the same code in two locations, but onEffect reads from disk more than I think is necessary. (I added some comments in the code)