S
SolidJS12mo ago
vveisard

How to create effect for each element of mapArray?

Hello, I'm exploring reactive patterns, and I'm stumped on how to create effects for elements of the result of mapArray. What I've got so far: https://github.com/vveisard/fiddle-solid-array-effect/blob/main/src/index.ts I'm thinking the return type of those memos ought to be Accessor<Array<Accessor<Array>>>, right? But how do I create that structure, and how to I create an effect that runs when the array element changes?
GitHub
fiddle-solid-array-effect/src/index.ts at main · vveisard/fiddle-so...
Contribute to vveisard/fiddle-solid-array-effect development by creating an account on GitHub.
3 Replies
thetarnav
thetarnav12mo ago
createMemo(mapArray(…)) mapArray returns a function, and keeps state between calls to diff the array not sure what youre going for but here are two examples of using mapArray with effects to do some side-effects base on what element got added/removed etc just some logging to show how reconcile works: https://playground.solidjs.com/anonymous/83da7ae2-a9cc-404c-a7ac-9fd487368460 updating a database based on changes to the store: https://github.com/kuskusapp/kuskus/blob/main/src/lib/primitives.ts#L7 https://github.com/kuskusapp/kuskus/blob/main/src/GlobalContext/todos.ts#L151
vveisard
vveisardOP12mo ago
Your explanation was very helpful, mapArray in createEffect seems like what I need. what I'm going for is running an effect when: - the value (reference) of the entity changes - the value of the counter property changes I've update my example: https://playground.solidjs.com/anonymous/075bb19e-581d-4476-aeaa-f226fbea12c6 However, because mapArray is non-tracking, it isn't behaving like I want.
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
vveisard
vveisardOP12mo ago
Solved it with an additional createEffect inside of mapArray.
Want results from more Discord servers?
Add your server