Skip to content

Any examples of using an array of objects? #682

Answered by franky47
mhddngs asked this question in Q&A
Discussion options

You must be logged in to vote

I'm getting nulls when trying to read from the array.

The purchasedItems type here is z.infer<typeof ItemSchema> | null, because there can be no item key in the URL. To prevent that, set a default value:

const [purchasedItems, setPurchasedItems] = useQueryState(
  "item",
  parseAsArrayOf(parseAsJson(ItemSchema.parse)).withDefault([])
);

Now be aware that URLs are user input: I don't know what your exact use-case is, but know that users can (and will) modify URLs, so in your case, they could add, remove, or edit purchased items.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mhddngs
Comment options

Answer selected by mhddngs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants