Skip to content

Commit ffbc8c1

Browse files
committed
feat(action): Error message for when calling raw action
i.e., when calling it without useAction
1 parent 5af3da8 commit ffbc8c1

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/data/action.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,40 @@ export function action<T extends Array<any>, U = void>(
6060
name?: string
6161
): Action<T, U> {
6262
function mutate(this: { r: RouterContext; f?: HTMLFormElement }, ...variables: T) {
63+
if (typeof this !== 'object' || !Object.hasOwn(this, 'r')) {
64+
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".
65+
66+
So, if you have code like this:
67+
68+
1 const myFunc = action(...);
69+
2
70+
3 / function MyComponent() {
71+
4 | return <button
72+
5 | onClick={() => myFunc(...)}
73+
|____________________^ This is where the error is going to happen
74+
6 >
75+
7 Click me!
76+
8 </button>
77+
9 }
78+
79+
You will need to change it to something like:
80+
81+
1 const myAction = action(...);
82+
2
83+
3 function MyComponent() {
84+
4 const callMyAction = useAction(myAction);
85+
5
86+
6 return <button
87+
7 onClick={() => callMyAction(...)}
88+
8 >
89+
9 Click me!
90+
10 </button>
91+
11 }
92+
93+
This is the case because the action will need to tune itself to the surrounding context for the Router so that it can
94+
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.
95+
`)
96+
}
6397
const router = this.r;
6498
const form = this.f;
6599
const p = (

0 commit comments

Comments
 (0)