Determining the return type for useActionData (validationErrors or actual data) #155
Unanswered
ryanwoodcox
asked this question in
Q&A
Replies: 2 comments 5 replies
-
Could you give an example of what you're trying to do? I'd expect the return type to be a union of |
Beta Was this translation helpful? Give feedback.
1 reply
-
Struggling with the same issue myself. We have an action that either returns a export const action = async ({ request }: DataFunctionArgs) => {
const data = await validator.validate(await request.formData());
if (data.error) return validationError(data.error);
const thing = await createThing(data.data);
return typedjson({ thing });
};
// in the component
const actionData = useTypedActionData<typeof action>();
useEffect(() => {
if (actionData?.thing) {
// Property 'thing' does not exist on type 'TypedResponse<ValidationErrorResponseData> | TypedJsonResponse<{ thing: Thing; }>'.
// Property 'thing' does not exist on type 'TypedResponse<ValidationErrorResponseData>'.ts(2339)
doSomethingWithSuccessResponse()
}
}, [actionData]); @ryanwoodcox We're you able to find a workaround? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When defining an action that can return either a
validationError(result.error)
or ajson(result.data)
, how should a developer go about determining which type is returned fromuseActionData<typeof action>()
?Beta Was this translation helpful? Give feedback.
All reactions