Passing hook values to loaders & actions #9547
-
The questionAs it stands, Auth0's React SDK library only allows token values to be accessed via the While a feature request like #9324 may cover this issue for passing context values, I'm unclear on how to handle this with values solely derived from hooks. Considering a project structure akin to Auth0's example app with React Router, how could the value from Related issueI've made the same request for addressing this in #9324. Double-sharing here, in the context of discussion, in case there are any additional approaches to consider. Similar questionsThis question has been asked in a couple of similar forms, but none quite for the specific use case I'm dealing with. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
I'm having the same problem, trying to figure it out how to do this correctly, but sadly I'm not finding a convenient solution. How are you solving it so far? @jordanthornquest. |
Beta Was this translation helpful? Give feedback.
-
You cannot access React context from loaders and actions because they are, by design, decoupled from the render cycle. Instead, you should aim to read from the source of truth for the data in question. Hooks are merely a convenience way to access a source of truth + update when it changes. With loaders/actions - RR will handle the "changes" part, so you just need to find the source of truth. I've been meaning to get a diagram of sorts together for this but basically:
Anything you read client-side in a hook comes from somewhere in the client - find that source and read from it in your loader. |
Beta Was this translation helpful? Give feedback.
-
Yeah, I think, it needs a little more context, to properly answer this question, the main problem is when the source of true is actually a hook, like in Auth0, where the access token generation happens inside of the hook, therefore is quite inconvenient not be able to have access to the context of the loader.
So in this scenario even if the intention is that the flow is unidirectional is quite obvious that you need the access token on the loader. Agree that this is also an So in my case in order to solve this issue I have to go as you mention one level up from the router and create a component that exposes this function and the user, since when you are making a request normally you will need both on the loader context. So my solution looks something like this: // AccessToken.jsx
export const AccessToken = () => {
const { getAccessTokenSilently, user } = useAuth0();
const getUser = () => user
return <>{ props.children(getAccessTokenSilently, getUser ) }</>
} // router.tsx
export const router = (accessToken) => {
return createBrowserRouter([
{
path: "/some-route",
loader: someLoader(tokenParameters),
},
])
} // main.jsx
import { AccessToken } from "./AccessToken"
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<Auth0Provider
domain="your-domain"
clientId="your-client-id"
authorizationParams={{
redirect_uri: `${window.location.origin}/your-route`,
}}
>
<AccessToken>
{(tokenParameters) => {
return <RouterProvider router={router(tokenParameters)} />
}}
</AccessToken>
</Auth0Provider>
</React.StrictMode>
I don't know if this it is a valid solution but it is one, so I haven't see any other example around here or auth0 documentation, also I didn't want to invest any more time on this. So let me know if there is any alternative to this scenario to me seems like there is not either Auth0 change its API or React Router provides a way to access hooks on loaders thanks for all the great work! |
Beta Was this translation helpful? Give feedback.
-
I found solutions without using closures in functions, as they complicate the code and add manual actions Using this solution you configure the router context from App. Then this context will be available as the second argument in the loader / action functions. This involves overriding some types in the @types/router.d.ts file. |
Beta Was this translation helpful? Give feedback.
I finally got around to putting together a basic Auth0 example using
RouterProvider
: https://github.com/brophdawg11/react-router-auth0-example