S
SolidJS•6mo ago
binajmen

How to set a new value in a store of `Record<string, __>`?

In the following example: https://playground.solidjs.com/anonymous/07b233f7-59cb-4202-a3ca-595a497912f8 setValue("abc", 3) on a blank object will trigger an error:
Uncaught TypeError: Cannot read properties of undefined (reading 'output')
Uncaught TypeError: Cannot read properties of undefined (reading 'output')
Probably because the internal of the Store's path syntax function is trying to access the data. Am I suppose to use the Immer inspired produce to add a new element to an object, or is there a syntax I don't know to use directly the setter from the store without using produce?
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
5 Replies
REEEEE
REEEEE•6mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
binajmen
binajmenOP•6mo ago
oh nice which one is better ? It seems for arrays suggestion are made in the doc (https://docs.solidjs.com/guides/complex-state-management#adding-to-an-array) but not for object should I give produce a priority? or rather, is one method more optimized than the other?
REEEEE
REEEEE•6mo ago
It could be that the syntax isn't supported internally for object but it is for arrays If you're going to do "complex" changes, I would just use produce
peerreynders
peerreynders•6mo ago
How is it supposed to now what to set input to?
const cloneWith = (v: Values | undefined, output: number) =>
v ? { input: v.input, output } : { input: 0, output };

function setValue(id: string, value: number) {
setValues(id, (old) => cloneWith(old, value));
}
const cloneWith = (v: Values | undefined, output: number) =>
v ? { input: v.input, output } : { input: 0, output };

function setValue(id: string, value: number) {
setValues(id, (old) => cloneWith(old, value));
}
i.e. input isn't marked as optional.
binajmen
binajmenOP•6mo ago
yes it was a reduced and incomplete example to illustrate my situation 😉
Want results from more Discord servers?
Add your server