-
-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(start): returning
null
from server functions (#3048)
Co-authored-by: SeanCassiere <[email protected]>
- Loading branch information
1 parent
3505a1f
commit 4a7d3ca
Showing
6 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
e2e/start/basic/app/routes/-server-fns/allow-fn-return-null.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* This exported component checks whether the server function can | ||
* return null without throwing an error or returning something else. | ||
* @link https://github.com/TanStack/router/issues/2776 | ||
*/ | ||
|
||
import * as React from 'react' | ||
import { createServerFn } from '@tanstack/start' | ||
|
||
const $allow_return_null_getFn = createServerFn().handler(async () => { | ||
return null | ||
}) | ||
const $allow_return_null_postFn = createServerFn({ method: 'POST' }).handler( | ||
async () => { | ||
return null | ||
}, | ||
) | ||
|
||
export function AllowServerFnReturnNull() { | ||
const [getServerResult, setGetServerResult] = React.useState<any>('-') | ||
const [postServerResult, setPostServerResult] = React.useState<any>('-') | ||
|
||
return ( | ||
<div className="p-2 border m-2 grid gap-2"> | ||
<h3>Allow ServerFn to return `null`</h3> | ||
<p> | ||
This component checks whether the server function can return null | ||
without throwing an error. | ||
</p> | ||
<div> | ||
It should return{' '} | ||
<code> | ||
<pre>{JSON.stringify(null)}</pre> | ||
</code> | ||
</div> | ||
<p> | ||
{`GET: $allow_return_null_getFn returns`} | ||
<br /> | ||
<span data-testid="allow_return_null_getFn-response"> | ||
{JSON.stringify(getServerResult)} | ||
</span> | ||
</p> | ||
<p> | ||
{`POST: $allow_return_null_postFn returns`} | ||
<br /> | ||
<span data-testid="allow_return_null_postFn-response"> | ||
{JSON.stringify(postServerResult)} | ||
</span> | ||
</p> | ||
<button | ||
data-testid="test-allow-server-fn-return-null-btn" | ||
type="button" | ||
className="rounded-md bg-white px-2.5 py-1.5 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50" | ||
onClick={() => { | ||
$allow_return_null_getFn().then(setGetServerResult) | ||
$allow_return_null_postFn().then(setPostServerResult) | ||
}} | ||
> | ||
Test Allow Server Fn Return Null | ||
</button> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters