Even if I do let indexToEdit = newList.findIndex(art => art.id === id + 1), it will edit the right one, but on the page refresh, it will edit the one before it?!
You can tell the engine how to do its job, or you can tell it what you want it to do for you. Your code is telling the js engine how to do its job. Wouldn’t something like artList.map(...) give better results?
// this replaces the splices, and merges the new record in place
const newList = artList.map( art => (art.id == id) ? {id, ...artBody} : art );
Doing this, I don’t need to know its position in the array, I don’t care how it handles the merge, I just find the one with the matching id, and insert my updated values in place. Boom done.
How is id reading the index of the array? id is being passed into your function. I’m assuming you’re calling that from some sort of listener, where is it getting the value it passes in as a param? Are you using the index of the row, or are you saving the art.id as (for example) a data-artid attribute on an HTML element?
@snowmonkey, very sorry to spam, but I found the issue looking through the React tools at the props. Found the id was set to 0 on the Art component. Instead of passing it through as id={idx}, I changed to id={art.id} and it works! Thank you so much for your second reply. I read it 5 more times then tackled it. Thank you!
Perfect! Great catch, and that’s exactly what I was wondering. Solid detective work on your part.
In future, if you find you’re stuck on a bigger project like this, it might be useful to link to the repository. I will often import repositories to repl, in order to test and debug (or, if they’re bigger and need things like a db, I pull them into my dev machine). But either way, seeing the full code is almost always a good thing.
Best of luck, and send me the link when you get it done!
ps. I noticed you’re using the map I showed you, how’s it working? Should be side-effect-less, so in theory, changing that becomes transparent.
However, I am in the process on a new branch (been a week and a half at least) integrating hooks, cleaning up the code entirely. There is a lot of mobile styling issues, etc. Bad code from a year ago. I’ll show repo when I’m done! I would really like input from you for how to even clean it up further when I am finished.
I am currently making a new thread for a question to switch this code over to hooks and losing my mind: