S
SolidJS3mo ago
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")
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
2 Replies
REEEEE
REEEEE3mo ago
You want to do const setElements = setStore.bind(null, "elements") There might be type issues though so you'll have to cast it using as SetStoreFunction<MySubType[]>
fm@bi
fm@bi3mo ago
That works like a charm, thank you!