What I would suggest is changing how your store the items in your UI state. For example, instead of storing an array of items, I would store an object, with the keys being the items id property:
setState(_.keyBy(items, 'id'))
Then, I would use your second idea (POST/PUT returns the updated resource). When you go to update the local state after the server responds to the request, you can easily update only the updated item:
webdev @ Autodesk |
Someone used to call me "Learn more", and I'm spending forever to live up to it. You'll find me dabbling in random stuff 👨💻 or missing a wide open shot in 🏀
Thanks! I was used to having an array before but the idea of using Objects is really growing on me. My app would most likely involve a lot of quick changes in the object's attribute, so this makes a lot of sense.
Amazing insight. The only problem I have with storing data in an object is the inability to loop through the items in that object. Using methods like "map" becomes practically impossible for a react developer.
How about writing a custom function? I think something like this would work and would create a nice mini-optimisation to use objects as you're suggesting!
function mapObject(obj, callback) {
for (let [idx, val] in obj.entries()) {
callback(val, idx);
}
}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Lenmor,
What I would suggest is changing how your store the items in your UI state. For example, instead of storing an array of items, I would store an
object, with the keys being the itemsidproperty:Then, I would use your second idea (POST/PUT returns the updated resource). When you go to update the local state after the server responds to the request, you can easily update only the updated item:
Thanks! I was used to having an array before but the idea of using Objects is really growing on me. My app would most likely involve a lot of quick changes in the object's attribute, so this makes a lot of sense.
Amazing insight. The only problem I have with storing data in an object is the inability to loop through the items in that object. Using methods like "map" becomes practically impossible for a react developer.
You can get around the loop issue by converting the object to an array before/during render:
How about writing a custom function? I think something like this would work and would create a nice mini-optimisation to use objects as you're suggesting!
function mapObject(obj, callback) {
for (let [idx, val] in obj.entries()) {
callback(val, idx);
}
}