You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
export a loader that conditionally throws an error or a response, but otherwise returns a JSON response
export a meta function which is accessing a data property, use MetaFunction to obtain data type
create an ErrorBoundary or CatchBoundary, depending on step 1
satisfy the condition that causes loader to throw
This is my slightly contrived app/routes/item.$num.tsx, which ensures that the $num param is a valid number, otherwise throws an error:
importtype{LoaderArgs,MetaFunction}from"@remix-run/node";import{json}from"@remix-run/node";import{useLoaderData}from"@remix-run/react";importinvariantfrom"tiny-invariant";exportasyncfunctionloader({ params }: LoaderArgs){invariant(params.num,"num is required");constnum=Number(params.num);invariant(!Number.isNaN(num),"num is not a number");returnjson({ num })}exportconstmeta: MetaFunction<typeofloader>=({ data })=>{return{title: `Item ${data.num}`,}}exportdefaultfunctionItem(){const{ num }=useLoaderData<typeofloader>()return(<h1>Item {num}</h1>)}exportfunctionErrorBoundary({ error }: {error: Error}){return(<div><h1>Something went wrong</h1><pre>{error.message}</pre></div>)}
Expected Behavior
I expected the data type in the meta function to stop me from unconditionally accessing data.num, assuming that data will always be an object.
Actual Behavior
The types in the meta function act like data will always be the object serialized from the loader, but in case the loader throws by accessing the root with an invalid $num, for example /item/foo, it will be undefined, so accessing data.num will throw an error.
The text was updated successfully, but these errors were encountered:
What version of Remix are you using?
1.7.4
Steps to Reproduce
loader
that conditionally throws an error or a response, but otherwise returns a JSON responsemeta
function which is accessing adata
property, useMetaFunction
to obtaindata
typeErrorBoundary
orCatchBoundary
, depending on step 1loader
to throwThis is my slightly contrived
app/routes/item.$num.tsx
, which ensures that the$num
param is a valid number, otherwise throws an error:Expected Behavior
I expected the
data
type in themeta
function to stop me from unconditionally accessingdata.num
, assuming thatdata
will always be an object.Actual Behavior
The types in the
meta
function act likedata
will always be the object serialized from theloader
, but in case theloader
throws by accessing the root with an invalid$num
, for example/item/foo
, it will beundefined
, so accessingdata.num
will throw an error.The text was updated successfully, but these errors were encountered: