Skip to content
Discussion options

You must be logged in to vote

Catch the error inside the loader and return a response, so your loader never throws, e.g.

export async function loader({ request }: LoaderFunctionArgs) {
  try {
    // do things here
    // send a success response
    return json({ ok: true as const, ...data })
  } catch (error) {
    // send a bad request response
    return json({ ok: false as const, error: (error as Error).message }, 400);
  }
}

Then in your component:

let { load, data } = useFetcher<typeof loader>()

if (!data) {
  return <button type="button" onClick={() => load("/resource/route")}>Load</button>
}

if (!data.ok) {
  return <p>Something failed: {data.message}</p>
}

if (data.ok) {
  return <p>Success! {data.something}<

Replies: 1 comment 1 reply

Comment options

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

Answer selected by ArvidAnderson
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