S
SolidJS•15mo ago
Nyantastic

Trying to update a value at index in a store array, updates all elements

Hello everyone, I'm sorry this is probably gonna be a bit long, I'm having a hard time trying to explain myself. Here is my issue: I'm working on an app using a context, and inside this context there is a store. I did like in the stores_context tutorial: I have a function that initializes a store, and returns it with some setter functions to update it. I have a useContext() function that returns the whole thing and it works. The store contains an object that contains an array of objects, and I'd like to update an attribute of the object at an index of the array. But when I call the function, the value is updated at all indexes, it seems. And this happens in the app only ; I have an unit test (with vitest) which calls exactly the same function, but it works fine and only the object at the right index is updated. I put the (very simplified) code in an attached file, I hope it worked. It's the function "setAttributeAtIndex" that causes issues. I've been console.logging it, it's called only once with the right index, I'm logging the context before and after the setMyContext. Right after the call, all of the indexes have been updated 😢 I also tried to change my setters and use the produce function, but it did exactly the same. I've been stuck on this for a while now, does anyone see something that I'm doing wrong? What should I do? Thanks a lot for your attention, I wish you a nice day!
9 Replies
Nyantastic
Nyantastic•15mo ago
I also found something else, I don't know if it will help or make it worse: In my unit tests, I also try to call the setObjectAtIndex function, like this: context.setObjectAtIndex (mockObject, 0); expect(context.myContext.myArray).toStrictEqual([ mockObject, ]); => Before this test, the array contained only one element, another mockObject, so it should be working. => The output of the test says, among other similar things: - Symbol(solid-proxy): [Object], + Symbol(solid-proxy): [Circular], But what would that mean? Any idea? I must have done something terribly wrong here. What bothers me is also the fact that everything was working fine when the store was global. But I wanted to change that to a context, and now I'm having those issues 🫠 I'd be eternally grateful if someone has any idea!
Unknown User
Unknown User•15mo ago
Message Not Public
Sign In & Join Server To View
Nyantastic
Nyantastic•15mo ago
Yes, the show is allowing me to have a loading state while i'm initializing my store, there are some async calls Or did you mean that I should use Suspense instead?
Unknown User
Unknown User•15mo ago
Message Not Public
Sign In & Join Server To View
Nyantastic
Nyantastic•15mo ago
Seems to be better practice yes, thank you!! Sadly it didn't fix my issue 😦
Unknown User
Unknown User•15mo ago
Message Not Public
Sign In & Join Server To View
Nyantastic
Nyantastic•15mo ago
That's a very good idea. I'll try that but it might take a while, I'll reach out when I have it! Thank you!
Nyantastic
Nyantastic•15mo ago
Hi, hope you had a nice weekend! Alright, this is the codesandbox I made to try to reproduce the bug: https://codesandbox.io/p/sandbox/jovial-fast-s7z0xs?file=%2Fsrc%2FApp.tsx&selection=%5B%7B%22endColumn%22%3A63%2C%22endLineNumber%22%3A12%2C%22startColumn%22%3A63%2C%22startLineNumber%22%3A12%7D%5D I apologize, some things must have been handled very poorly, but the bug is there. I must have done something stupid to have this bug, but honestly I don't know where to look anymore. Maybe you'll spot the mistake! In my example, I just have two buttons, each one uses the context. On click on one button, the function "setAttributeAtIndex" in the context is called, with the right index, and is supposed to update the count value. I wish it would update only the one the user clicked, but it updates both counts, at both indexes.
jovial-fast-s7z0xs
CodeSandbox is an online editor tailored for web applications.
Nyantastic
Nyantastic•15mo ago
Maybe useContext isn't supposed to be called twice?
Want results from more Discord servers?
Add your server
More Posts
Redirect to current location from createServerAction$()I have a logout action that I would like to logout the user without changing the location. My currenlooking for better solid solutionHi I have this app that I making about create a food menu. I want to add to my list of foods for speQuestions on the `owner` of using children() helper inside a contextI'm writing a component to show Toast. The idea is to wrap the main component inside a ToastContextcreateStore array containing arraysI have a store that is an array of objects where some properties are arrays. Then I do <For each={Track Variable Changesi quite don't remember a lot the splid js syntax can someone explain to me how can i track variable Netlify Fails to accept `dist`I have many sites on Netlify from HUGO, and this is my first SolidJS. I ran `npm run build` and creaIs there a way to hook into the router's match (client)?Is there a way to hook into the router (client-side) to override `router.location` in the `<Routes>`What to save to database with authjs to preserve privacy?Basically authjs with most oauth providers gives for example name or email as a scope. But I dont waCannot reference properties of object stored in a signal after being fetched from APII fetch some data via a REST API and store it in a signal within the `onMount` hook. The signal sto`crossOrigin` and `playsInline` types don't work with solid & typescriptWhen running `tsc --jsx preserve -t es2020 --outDir js --noEmit false` on my solidjs site, I get the