kiliman / remix-typedjson

This package is a replacement for superjson to use in your Remix app. It handles a subset of types that `superjson` supports, but is faster and smaller.
MIT License
435 stars 19 forks source link

Typed JSON while submitting data to the server? #40

Closed abustany closed 6 months ago

abustany commented 6 months ago

useTypedFetcher allows getting correctly typed data from the server to the frontend, however it does not seem to be helping when calling fetcher.submit with an object as the target? It feels like we could use the current encoding for "complex" values not only for receiving data, but also for sending data to the server?

abustany commented 6 months ago

Aha, looks like I can simply use the serialize and deserialize methods, like this:

export async function action({request}: ActionFunctionArgs) {
  const data = deserialize(await request.json());
}

export default Component() {
  const fetcher = useFetcher();
  fetcher.submit(serialize(myObject), {encType: "application/json"});
}
kiliman commented 6 months ago

I was just going to comment on that. Glad you figured it out.