How does `Array.prototype.slice.call` work?
I'm learning react and I'm trying to create a cart for an e-commerce app.
In my tutorial, we are creating the cart using useContext (because we will need it in several places in our application). However, when I was learning JavaScript vanilla, I created another e-commerce app and I used the localStorage for my cart.
So my question: Is it a bad practice to use localStorage instead of useContext in React? Which is better? In which cases?
Context
can be reactive on changes, so you can update its value and see the change in all components subscribed, however with localStorage yo can't directly.
Think about your feature, do you need persist the data when user close the navigator? use localStorage
or do you need handle data dynamically through different components? then use Context
PD: Also, you can use both, localStorage
would be used on mount and unmount events, and Context
would be used in the rest of life cycle app.
localStorage
is persistent. The data will be available after you refresh your browser. It is saved locally in browser.
context
is not persistent. The data will be gone after you refresh.
Which is better? They are two different tools. Maybe can use both, every time you visit the web, check if there is something in localStorage
, then add it to context
.