SolidJSS
SolidJSโ€ข2y agoโ€ข
3 replies
fm@bi

setStore with partial application

Hello, I've recently started using createStore a bit and have a question (coming from a functional programming background).

I have a setup that is basically a store of an object containing an array, and I want to pass a setter for the array specifically to a child component. Consider this snippet:
type MySubType = {
  a: string
  b: number
}
type MyType = {
  elements: MySubType[]
}
const [store, setStore] = createStore<MyType>({elements: [{a: "foo", b: 123}]})
// This works nicely.
setStore("elements", 0, "a", "bar")

// This does not. :(
// Argument of type 'string' is not assignable to parameter of type 'StoreSetter<MyType, []>'.ts(2345)
const setElements = setStore("elements")
setElements(0, "a", "bar")

As you can see I am trying to use partial application to create a setter for a property of my store, in order to use that with all its SetStoreFunction advantages. Is such a thing supported or do I need to build a workaround for this usecase?

As a sidenote, is it fine to pass store and setStore to a child component to update my state or is that not recommended?
Thanks, have a good one
Was this page helpful?