Skip to content
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

useLoaderData returns undefined when used in root.tsx #9717

Closed
npapagna opened this issue Jul 5, 2024 · 5 comments
Closed

useLoaderData returns undefined when used in root.tsx #9717

npapagna opened this issue Jul 5, 2024 · 5 comments

Comments

@npapagna
Copy link
Contributor

npapagna commented Jul 5, 2024

Reproduction

  1. Go to https://stackblitz.com/edit/remix-run-remix-cuwoyi?file=app%2Froutes%2F_index.tsx
  2. Notice that both root.tsx and _index.tsx render undefined as the output of the useActionData hook (see rendered messages on the page.
  3. Click the Click to submit button to call the clientAction form the _index.tsx route.
  4. Notice that useActionData returns {toast: "foo"} in the _index.tsx route, but returns undefined in root.tsx (see rendered messages on the page.

System Info

System:
    OS: macOS 13.6.7
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 25.92 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.16.0/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
    pnpm: 8.14.0 - /usr/local/bin/pnpm
  Browsers:
    Brave Browser: 124.1.65.126
    Chrome: 126.0.6478.127
    Safari: 16.6
  npmPackages:
    @remix-run/dev: ^2.9.2 => 2.9.2 
    @remix-run/node: ^2.9.2 => 2.9.2 
    @remix-run/react: ^2.9.2 => 2.9.2 
    vite: ^5.1.0 => 5.2.12

Used Package Manager

npm

Expected Behavior

I was expecting to access the (the serialized data from the most recent route action)[https://remix.run/docs/en/main/hooks/use-action-data] in root.tsx to implement an Toasts component.

The clientAction on _index.tsx would do stuff and return {toast: "message"} as part of the response (other routes do this as well) and in root.tsx I wanted to render the toasts (when present).

This is a Remix SPA project (that's why we use a clientAction instead of action).

Actual Behavior

useActionData returns the clientAction response only in the route where the client action is defined.

@kiliman
Copy link
Collaborator

kiliman commented Jul 8, 2024

In Remix, the actual behavior is the correct behavior. useActionData only returns the action response from the route it's defined in.

Typically things like toast messages would be set via session.flash that the root loader would access.

Since you're creating a SPA, you may want to use something like react-hot-toast where you can call the toast function from anywhere, including the clientAction

export async function clientAction() {
  // call the toast function directly
  toast('hello remix');
  return null;
}

https://stackblitz.com/edit/remix-run-remix-bt6c8m?file=app%2Froot.tsx,app%2Froutes%2F_index.tsx

@brophdawg11
Copy link
Contributor

@kiliman is correct here - useActionData (like useLoaderData) returns the action data for the contextual route - not for any route.

@brophdawg11 brophdawg11 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 8, 2024
@npapagna
Copy link
Contributor Author

npapagna commented Jul 8, 2024

Thanks for the suggestion @kiliman!

@brophdawg11 in that case I feel the docs are a bit unclear, since they mention that:

Returns the serialized data from the most recent route action or undefined if there isn't one.

That made me think I could useActionData in root.tsx initially.
Maybe it can be updated to clarify that it only returns the action response from the route it's defined in?

@brophdawg11
Copy link
Contributor

PRs are always welcome to clarify the docs! Would you like to push a PR with a proposed clearer wording?

npapagna added a commit to npapagna/remix that referenced this issue Jul 12, 2024
The current documentation for the `useActionData` hook might
unintentionally imply that it can be used to access data from any route
action. However, the hook is limited to accessing data associated with
the current route action where it's called.

This PR clarifies this limitation in the documentation to prevent
confusion.

See remix-run#9717 for more information.
@npapagna
Copy link
Contributor Author

npapagna commented Jul 12, 2024

PRs are always welcome to clarify the docs! Would you like to push a PR with a proposed clearer wording?

@brophdawg11 sure thing! #9744

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants