Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

README.md missleading: Primitive Store can only be used with strings #44

Open
RaphaelBossek opened this issue Jan 22, 2024 · 5 comments

Comments

@RaphaelBossek
Copy link

The Primitive Store example and the explanation from README.md suggest that persistentAtom also allows types other than string, which is not the case for the type definition

@ai
Copy link
Member

ai commented Jan 22, 2024

Are you talking about this example:

import { persistentAtom } from '@nanostores/persistent'

export const shoppingCart = persistentAtom<Product[]>('cart', [], {
  encode: JSON.stringify,
  decode: JSON.parse,
})

It has encode/decode options to convert object to string and back.

What exact problem do you have?

@ivan-suhorukov
Copy link

ivan-suhorukov commented Jan 23, 2024

I issued the same problem then i tried to use persistendAtom without third argument. Maybe there will be a clener code if you make it JSON encode/decode by default without need to use a third argument?

@ai
Copy link
Member

ai commented Jan 23, 2024

@ivan-suhorukov JSON.parse is not the best option for serialisation since we may also need validation, etc.

Do you use TypeScript? It should warn you.

@RaphaelBossek
Copy link
Author

I've created a small project at https://codesandbox.io/p/github/RaphaelBossek/nanostores-persistent-example/main?file=%2Fsrc%2Fcounter.ts%3A16%2C1 for the TypeScript issue with the example:
image

@ai
Copy link
Member

ai commented Jan 23, 2024

TS error looks good, it highlights need of encoding.

How do you suggest changing the doc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants