paoloricciuti / sveltekit-search-params

The easiest way to read and WRITE from query parameters in sveltekit.
https://sveltekit-search-params.netlify.app
MIT License
549 stars 19 forks source link

Usage in derived storage #30

Closed ewal closed 1 year ago

ewal commented 1 year ago

Describe the problem

I would like to do some computations in a derived store based on values returned from queryParameters but can't figure out how. I guess that it may currently not be possible based on how queryParameters is exported? Currently it's exported as a function but the derived store expects a writable store as an argument.

Describe the proposed solution

It would be great to have an exported writable store that could be used as such:

export const derivedStore = derived([queryParameters, anotherStore], ([$queryParameters, $anotherStore]) => {
   const { param1, param2, ... } = $queryParameters;
   // ... 
});
paoloricciuti commented 1 year ago

Calling the function returns a writable store that you can pass to the derived. What kind of calculations you need to do?

ewal commented 1 year ago

I have tried calling the queryParameters function in the derived store but I get the following error:

Error: Cannot subscribe to 'page' store on the server outside of a Svelte component, as it is bound to the current request via component context. This prevents state from leaking between users.For more information, see https://kit.svelte.dev/docs/state-management#avoid-shared-state-on-the-server

(I basically want to filter a store based on the values in the query params).

ewal commented 1 year ago

I found a solution by subscribing to the queryParameters() store from where I update another store that can used in the derived store. Bit of a overhead perhaps but works fine :+1:

paoloricciuti commented 1 year ago

Than its a different problem: you are trying to subscribe to the store in the load function but this is a security issue.

You should just use this store inside a svelte component

ewal commented 1 year ago

Strange, because I got the error when I tried to use the queryParameters store inside a derived store :thinking: (Not subscribing to it, that is). Anyway, I kinda realized I have all I need inside the $page object so I will close this issue now... Thank you for your replies.