Skip to content

feat(action): Error message for when calling raw action #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-rocks-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

Add an error message to when calling an action without first wrapping it in `useAction`
34 changes: 34 additions & 0 deletions src/data/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,40 @@ export function action<T extends Array<any>, U = void>(
name?: string
): Action<T, U> {
function mutate(this: { r: RouterContext; f?: HTMLFormElement }, ...variables: T) {
if (typeof this !== 'object' || !Object.hasOwn(this, 'r')) {
throw new Error(`Seems like you are directly calling a function wrapped in "action". To properly use an action, you will need to first call "useAction".

So, if you have code like this:

1 const myFunc = action(...);
2
3 / function MyComponent() {
4 | return <button
5 | onClick={() => myFunc(...)}
|____________________^ This is where the error is going to happen
6 >
7 Click me!
8 </button>
9 }

You will need to change it to something like:

1 const myAction = action(...);
2
3 function MyComponent() {
4 const callMyAction = useAction(myAction);
5
6 return <button
7 onClick={() => callMyAction(...)}
8 >
9 Click me!
10 </button>
11 }

This is the case because the action will need to tune itself to the surrounding context for the Router so that it can
keep track of all the submissions. See https://docs.solidjs.com/solid-router/concepts/actions#creating-actions for more information on how to use actions.
`)
}
const router = this.r;
const form = this.f;
const p = (
Expand Down