From ffbc8c1ca49267c52c5a49d0cacd03151a038e82 Mon Sep 17 00:00:00 2001 From: gabriel miranda Date: Sat, 13 Apr 2024 17:07:42 -0300 Subject: [PATCH 1/2] feat(action): Error message for when calling raw action i.e., when calling it without useAction --- src/data/action.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/data/action.ts b/src/data/action.ts index ec8b6fa61..fb27a4517 100644 --- a/src/data/action.ts +++ b/src/data/action.ts @@ -60,6 +60,40 @@ export function action, U = void>( name?: string ): Action { 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 +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 +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 = ( From 0504bbbfd7c247cd42b5f03f0e8d9447795bc9a7 Mon Sep 17 00:00:00 2001 From: gabriel miranda Date: Sat, 13 Apr 2024 17:18:53 -0300 Subject: [PATCH 2/2] chore: add changeset --- .changeset/bright-rocks-share.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/bright-rocks-share.md diff --git a/.changeset/bright-rocks-share.md b/.changeset/bright-rocks-share.md new file mode 100644 index 000000000..c1ef65d42 --- /dev/null +++ b/.changeset/bright-rocks-share.md @@ -0,0 +1,5 @@ +--- +"@solidjs/router": patch +--- + +Add an error message to when calling an action without first wrapping it in `useAction`