Comments (2)
Yeah we can do it, for time being, you can refer this component from one of my other project:
componentDidMount() {
this.hydrateStateWithLocalStorage();
}
handleAddNew = (id, message, isCompleted) => {
let newNote = { id, message, isCompleted };
let notes = [...this.state.notes, newNote];
this.setState({ notes });
localStorage.setItem("notes", JSON.stringify(notes));
}
handleStatusUpdate = (id, isCompleted) => {
let notes = [...this.state.notes];
let index = notes.findIndex(item => item.id === id);
notes[index].isCompleted = isCompleted;
this.setState({ notes });
localStorage.setItem("notes", JSON.stringify(notes));
console.log(JSON.parse(localStorage.getItem('notes')), null, 2);
}
handleMessageUpdate = (id, message) => {
let notes = [...this.state.notes];
let index = notes.findIndex(item => item.id === id);
notes[index].message = message;
this.setState({ notes });
localStorage.setItem("notes", JSON.stringify(notes));
}
handleDelete = (id) => {
let notes = [...this.state.notes];
let index = notes.findIndex(item => item.id === id);
notes.splice(index, 1);
this.setState({ notes });
localStorage.setItem("notes", JSON.stringify(notes));
}
hydrateStateWithLocalStorage() {
// for all items in state
for (let key in this.state) {
// if the key exists in localStorage
if (localStorage.hasOwnProperty(key)) {
// get the key's value from localStorage
let value = localStorage.getItem(key);
// parse the localStorage string and setState
try {
value = JSON.parse(value);
this.setState({ [key]: value });
} catch (e) {
// handle empty string
this.setState({ [key]: value });
}
}
}
}
I will update the same feature in react-shopping-cart in few days!
from react-shopping-cart.
Great. Thanks
from react-shopping-cart.
Related Issues (20)
- add paypal method HOT 27
- Why bind class methods HOT 4
- Demo not working? HOT 1
- Unable to run shopping cart in localhost HOT 7
- Avoid error: toLowerCase() doesn't exist for undefined and/or similar errors
- missing script: start HOT 1
- Can we use react-redux for State Management? HOT 1
- Upload images
- bug need fix HOT 2
- Blank page on IE 11 HOT 2
- CSRF question
- Products not loading while trying to run the project locally HOT 1
- typo error while adding product in quick view
- sum error in shopping cart
- Image upload
- Mathematical Error while adding and Subtracting the product.
- Installation fails HOT 1
- Couldn't install node modul HOT 1
- post a license for this software HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-shopping-cart.